go-logging
go-logging copied to clipboard
Duplicate print in log
go 1.9.2
logger := logging.MustGetLogger("main")
format := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05.000} [%{level}] %{message}`)
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
backendLevel := logging.AddModuleLevel(backend)
backendLevel.SetLevel(logging.DEBUG, "")
logging.SetBackend(backendLevel, backendFormatter)
logger.Debug("TestDebug")
logger.Warning("TestWarning")
2018-03-29 12:17:04.794 [DEBUG] TestDebug
TestWarning
2018-03-29 12:17:04.794 [WARNING] TestWarning
such behavior starting with the level WARNING Why?
@shamanis i thank the readme must have confused a lot of prgrammers:
1. Why the output is duplicate
2. How to set logging level
The answer to both questions is that the readme "example" has two backend! Just look into the logging.SetBeckend func
func SetBackend(backends ...Backend) LeveledBackend {
var backend Backend
if len(backends) == 1 {
backend = backends[0]
} else {
backend = MultiLogger(backends...)
}
defaultBackend = AddModuleLevel(backend)
return defaultBackend
}
So, just change your code as the following,
logger := logging.MustGetLogger("main")
format := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05.000} [%{level}] %{message}`)
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
logging.SetBackend(backendFormatter)
logging.SetLevel(logging.DEBUG, "main")
logger.Debug("TestDebug")
logger.Warning("TestWarning")
And if want to change log level, just
logging.SetLevel(logging.xxx, "main")
Yes I really think the readme should have a really simple example, and another where they show us an example with multiple backends, but other than that I love this lib!
It would be nice if more samples provided.