events icon indicating copy to clipboard operation
events copied to clipboard

Request: Improve logging for custom types

Open f2prateek opened this issue 8 years ago • 0 comments

Apex has an interface called log.Fielder https://godoc.org/github.com/apex/log#Fielder. This lets custom types conform to this interface. For instance the struct:

type Foo struct {
        ID string
}

func (f Foo) Fields() log.Fields {
	return log.Fields{"id": i.ID}
}

Can now be easily logged, and will be correctly destructured in the logs.

log.WithFields(foo).Info("upload")

The equivalent with the events package is:

events.Log("upload", events.Args{
	{"id", foo.ID},
})

This becomes verbose as more fields are added to the struct, and it is logged in multiple places.

Another downside of destructuring by hand is that leads to inconsistency over time. The same struct might be logged differently in different places as new fields are added.

Filed as a request as I don't have a proposal for an API or implementation yet.

f2prateek avatar Jul 31 '17 18:07 f2prateek