Logrus: How do we print field names when using structs with "WithFields" ?

Created on 15 Feb 2017  路  4Comments  路  Source: sirupsen/logrus

For the following snippet of code :

package main

import(
        log "github.com/Sirupsen/logrus"
)

type A struct {
        a int
        b string
}

func main() {
        z := A{a: 1, b: "Test"}
        log.Infof("%+v", z)
        log.WithFields(log.Fields{"A": z}).Info("Value is ")
}

The output is

INFO[0000] {a:1 b:Test}
INFO[0000] Value is                                      A={1 Test}

What do I do with "WithFields" so that in the second line, the output shows fieldnames as well ?
i.e. I would like the output to be

INFO[0000] {a:1 b:Test}
INFO[0000] Value is                                      A={a:1 b:Test}

We are trying to standardize on using "WithFields" in our codebase, hence the question.

Most helpful comment

I do this (what prev comment was alluding to I think):

    log.WithFields(log.Fields{
        "user": fmt.Sprintf("%+v", u),
    }).Debug("Found User")

All 4 comments

Implement Stringer interface for this type.

Thanks !
It gets tedious for a large project with many many types ....

Look at fmt.Sprintf

I do this (what prev comment was alluding to I think):

    log.WithFields(log.Fields{
        "user": fmt.Sprintf("%+v", u),
    }).Debug("Found User")
Was this page helpful?
0 / 5 - 0 ratings