go-logging
go-logging copied to clipboard
*Logger type uses global backend in IsEnabledFor method
Whenever a new logger is created; it defaults to calling the global defaultBackend
rather than it's own backend
.
- https://github.com/op/go-logging/blob/master/logger.go#L138
func (l *Logger) IsEnabledFor(level Level) bool {
return defaultBackend.IsEnabledFor(level, l.Module)
}
This causes for the backend defined by the SetBackend
method to be ignored unless set with the package method SetBackend
; i.e:
package main
import (
"os"
"github.com/op/go-logging"
)
func main() {
formatter := logging.MustStringFormatter("JUST A PREFIX: ${message}")
backend := logging.NewLogBackend(os.Stdout, "", log.LstdFlags)
lg := logging.MustGetLogger("example")
backendFormatter := logging.NewBackendFormatter(backend, formatter)
leveled := logging.AddModuleLevel(backendFormatter)
leveled.SetLevel(logging.DEBUG, "")
// The following will cause for the backend `leveled` to be ignored by `lg`
lg.SetBackend(leveled)
// This will set the backend globally
logging.SetBackend(leveled)
}