Root logger logs debug messages by default
kevinburke opened this issue · comments
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.
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.
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:
-
Keep a reference to the handler (
nextHandler
) that you pass into theLvlHandler
that filters error messages and create a newLvlHandler(log15.LvlDebug, nextHandler)
when you want to switch -
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)