inconshreveable / log15

Structured, composable logging for Go
https://godoc.org/github.com/inconshreveable/log15
Other
1.1k stars 144 forks source link

Root logger logs debug messages by default #111

Closed kevinburke closed 8 years ago

kevinburke commented 8 years ago

If I do

l := log.New()
l.Debug("test message")

I'd expect that message to not print, e.g. I'd expect to have to configure something to enable debug logs. But it gets printed.

kevinburke commented 8 years ago

Another problem - if a Logger is configured to only log Error messages, I'm not sure how to undo that setting. E.g. if I do

l.SetHandler(log15.LvlHandler(log15.LvlDebug, l.GetHandler()))

I believe debug messages still won't be printed since the error handler is still present/running.

inconshreveable commented 8 years ago

I'm okay with debug messages printing by default. I think it's less confusing then the alternative getting-started experience of calling Debug(), getting no output and then wondering why it's not working. Breaking backwards compatibility on this is probably not worth it.

You're correct that you can't change the logging level like that. Instead you'd need to do one of two things:

1) Keep a reference to the handler (nextHandler) that you pass into the LvlHandler that filters error messages and create a new LvlHandler(log15.LvlDebug, nextHandler) when you want to switch

2) Create your own filtering handler that you can dynamically change at runtime:

var lvl atomic.Value
lvl.Store(log15.LvlDebug)
l.SetHandler(log15.FilterHandler(func(r *log15.Record) bool {
  return lvl <= lvl.Load().(*log15.Lvl)
}))

// later
lvl.Store(log15.LvlError)