ctxlog
is a library which helps to add a contextual data to your log messages at any time, and have it logged with each message.
TBD but be aware it might happen
- Remove dependency on Patron and replace it with some generic logger
- Add examples in
examples/
folder
To add this library as a dependency of your project, run
$ go get github.com/beatlabs/ctxlog
Initiate a logger in a request context
req := &http.Request{}
ctx := ctxlog.AddLoggerForRequest(req)
Add some custom data to it
ctxlog.FromContext(ctx).Int("answer", 42)
ctxlog.FromContext(ctx).Str("so", "long")
ctxlog.FromContext(ctx).SubCtx(map[string]interface{}{
"thanks_for": "all the fish",
"planet": "Earth",
"happened_on": "Thursday",
})
And log it
ctxlog.FromContext(ctx).Warnf("this %s has — or rather had — a %s, which was this: %s", "planet", "problem",
"most of the people living on it were unhappy for pretty much all of the time")