2015-10-21 18:19:14 -07:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2015-10-22 12:33:53 -07:00
|
|
|
. "github.com/tendermint/go-common"
|
2015-10-21 18:19:14 -07:00
|
|
|
"github.com/tendermint/log15"
|
|
|
|
)
|
|
|
|
|
2015-12-09 09:38:40 -08:00
|
|
|
//var rootHandler log15.Handler
|
|
|
|
var mainHandler log15.Handler
|
|
|
|
var bypassHandler log15.Handler
|
2015-10-21 18:19:14 -07:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2016-02-05 15:00:03 -08:00
|
|
|
func SetLogLevel(logLevel string) {
|
|
|
|
resetWithLogLevel(logLevel)
|
|
|
|
}
|
2015-10-21 18:19:14 -07:00
|
|
|
|
2016-02-05 15:00:03 -08:00
|
|
|
func resetWithLogLevel(logLevel string) {
|
2015-12-09 09:38:40 -08:00
|
|
|
// main handler
|
2015-10-21 18:19:14 -07:00
|
|
|
//handlers := []log15.Handler{}
|
2015-12-09 09:38:40 -08:00
|
|
|
mainHandler = log15.LvlFilterHandler(
|
2015-10-21 18:19:14 -07:00
|
|
|
getLevel(logLevel),
|
|
|
|
log15.StreamHandler(os.Stdout, log15.TerminalFormat()),
|
|
|
|
)
|
2015-12-09 09:38:40 -08:00
|
|
|
//handlers = append(handlers, mainHandler)
|
|
|
|
|
|
|
|
// bypass handler for not filtering on global logLevel.
|
|
|
|
bypassHandler = log15.StreamHandler(os.Stdout, log15.TerminalFormat())
|
|
|
|
//handlers = append(handlers, bypassHandler)
|
2015-10-21 18:19:14 -07:00
|
|
|
|
|
|
|
// Set rootHandler.
|
|
|
|
//rootHandler = log15.MultiHandler(handlers...)
|
|
|
|
|
|
|
|
// By setting handlers on the root, we handle events from all loggers.
|
2015-12-09 09:38:40 -08:00
|
|
|
log15.Root().SetHandler(mainHandler)
|
|
|
|
}
|
|
|
|
|
2016-02-05 15:00:03 -08:00
|
|
|
// You might want to call this after resetting tendermint/go-config.
|
|
|
|
func Reset() {
|
|
|
|
|
|
|
|
var logLevel string = "debug"
|
|
|
|
if config != nil {
|
|
|
|
logLevel = config.GetString("log_level")
|
|
|
|
}
|
|
|
|
|
|
|
|
resetWithLogLevel(logLevel)
|
|
|
|
}
|
|
|
|
|
2015-12-09 09:38:40 -08:00
|
|
|
// See go-wire/log for an example of usage.
|
|
|
|
func MainHandler() log15.Handler {
|
|
|
|
return mainHandler
|
2015-10-21 18:19:14 -07:00
|
|
|
}
|
|
|
|
|
2015-12-09 09:38:40 -08:00
|
|
|
func BypassHandler() log15.Handler {
|
|
|
|
return bypassHandler
|
2015-10-21 18:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx ...interface{}) log15.Logger {
|
2015-12-09 09:38:40 -08:00
|
|
|
return NewMain(ctx...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMain(ctx ...interface{}) log15.Logger {
|
2015-10-21 18:19:14 -07:00
|
|
|
return log15.Root().New(ctx...)
|
|
|
|
}
|
|
|
|
|
2015-12-09 09:38:40 -08:00
|
|
|
func NewBypass(ctx ...interface{}) log15.Logger {
|
|
|
|
bypass := log15.New(ctx...)
|
|
|
|
bypass.SetHandler(bypassHandler)
|
|
|
|
return bypass
|
|
|
|
}
|
|
|
|
|
2015-10-21 18:19:14 -07:00
|
|
|
func getLevel(lvlString string) log15.Lvl {
|
|
|
|
lvl, err := log15.LvlFromString(lvlString)
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Invalid log level %v: %v", lvlString, err))
|
|
|
|
}
|
|
|
|
return lvl
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// Exported from log15
|
|
|
|
|
|
|
|
var LvlFilterHandler = log15.LvlFilterHandler
|
|
|
|
var LvlDebug = log15.LvlDebug
|
|
|
|
var LvlInfo = log15.LvlInfo
|
|
|
|
var LvlWarn = log15.LvlWarn
|
|
|
|
var LvlError = log15.LvlError
|