Skip to content

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.

License

Notifications You must be signed in to change notification settings

beatlabs/ctxlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5ca9691 · Nov 8, 2022

History

4 Commits
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 7, 2022
Nov 8, 2022
Nov 8, 2022
Nov 7, 2022

Repository files navigation

Context Logger CI

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.

Short-term plans

TBD but be aware it might happen

  • Remove dependency on Patron and replace it with some generic logger
  • Add examples in examples/ folder

Install it

To add this library as a dependency of your project, run

$ go get github.com/beatlabs/ctxlog

Usage examples

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")