3 ms·
Libraries using log.Printf are incredibly tough to work with in a production environment if you want anything more interesting than looking at stderr. Libraries
by hesdeadjim 10y ago
Libraries using log.Printf are incredibly tough to work with in a production environment if you want anything more interesting than looking at stderr. Libraries that let you provide a logger API at time of construction are better than log.Printf, but they still fail at letting you include contextual information inside method calls.
We inject a logging interface into all our methods that take a context.Context object [1]. This allows us to push contextual information onto the stack of context objects, and then when we log we can do it at the point of failure and have access to an immense amount of useful information. Check the attached gist to see an example of how this works in practice [2].
Given that the context library originated out of Google and is now part of the stdlib in 1.7, I would love to see other libraries embrace it instead of relying on much less flexible solutions.
[1] https://godoc.org/golang.org/x/net/context https://godoc.org/golang.org/x/net/context
[2] https://gist.github.com/justonia/f81eead323d2b23eca1c485ed8e5db5d https://gist.github.com/justonia/f81eead323d2b23eca1c485ed8e...