2015-01-08 17:24:04 -08:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/tendermint/log15"
|
2015-01-20 15:31:31 -08:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-01-08 17:24:04 -08:00
|
|
|
"github.com/tendermint/tendermint/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootHandler log15.Handler
|
|
|
|
|
2015-01-20 15:31:31 -08: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
|
|
|
|
}
|
|
|
|
|
2015-01-08 17:24:04 -08:00
|
|
|
func init() {
|
|
|
|
handlers := []log15.Handler{}
|
|
|
|
|
|
|
|
// By default, there's a stdout terminal format handler.
|
|
|
|
handlers = append(handlers, log15.LvlFilterHandler(
|
2015-01-20 15:31:31 -08:00
|
|
|
getLevel(config.App.GetString("Log.Stdout.Level")),
|
2015-01-08 17:24:04 -08:00
|
|
|
log15.StreamHandler(os.Stdout, log15.TerminalFormat()),
|
|
|
|
))
|
|
|
|
|
|
|
|
// Maybe also write to a file.
|
2015-01-20 15:31:31 -08:00
|
|
|
if _logFileDir := config.App.GetString("Log.File.Dir"); _logFileDir != "" {
|
2015-01-08 17:24:04 -08:00
|
|
|
// Create log dir if it doesn't exist
|
2015-01-20 15:31:31 -08:00
|
|
|
err := os.MkdirAll(_logFileDir, 0700)
|
2015-01-08 17:24:04 -08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not create directory: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
// File handler
|
|
|
|
handlers = append(handlers, log15.LvlFilterHandler(
|
2015-01-20 15:31:31 -08:00
|
|
|
getLevel(config.App.GetString("Log.File.Level")),
|
|
|
|
log15.Must.FileHandler(_logFileDir+"/tendermint.log", log15.LogfmtFormat()),
|
2015-01-08 17:24:04 -08:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set rootHandler.
|
|
|
|
rootHandler = log15.MultiHandler(handlers...)
|
|
|
|
|
|
|
|
// By setting handlers on the root, we handle events from all loggers.
|
|
|
|
log15.Root().SetHandler(rootHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See binary/log for an example of usage.
|
|
|
|
func RootHandler() log15.Handler {
|
|
|
|
return rootHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx ...interface{}) log15.Logger {
|
|
|
|
return log15.Root().New(ctx...)
|
|
|
|
}
|