2017-09-15 16:00:00 +00:00
|
|
|
/*Package log implements logger for status-go.
|
|
|
|
|
|
|
|
This logger handles two loggers - it's own and ethereum-go logger.
|
|
|
|
Both are used as "singletons" - using global shared variables.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
|
|
|
First, import package into your code:
|
|
|
|
|
|
|
|
import "github.com/status-im/status-go/geth/log
|
|
|
|
|
|
|
|
Then simply use `Info/Error/Debug/etc` functions to log at desired level:
|
|
|
|
|
|
|
|
log.Info("Info message")
|
|
|
|
log.Debug("Debug message")
|
|
|
|
log.Error("Error message")
|
|
|
|
|
|
|
|
Slightly more complicated logging:
|
|
|
|
|
|
|
|
log.Warn("abnormal conn rate", "rate", curRate, "low", lowRate, "high", highRate)
|
|
|
|
|
|
|
|
Note, in this case parameters should be in in pairs (key, value).
|
|
|
|
|
|
|
|
This logger is based upon log15-logger, so see its documentation for advanced usage: https://github.com/inconshreveable/log15
|
|
|
|
|
|
|
|
|
|
|
|
Initialization
|
|
|
|
|
|
|
|
By default logger is set to log to stdout with Error level via `init()` function.
|
|
|
|
You may change both level and file output by `log.SetLevel()` and `log.SetLogFile()` functions:
|
|
|
|
|
|
|
|
log.SetLevel("DEBUG")
|
|
|
|
log.SetLogFile("/path/to/geth.log")
|
|
|
|
|
|
|
|
*/
|
2017-08-10 13:35:58 +00:00
|
|
|
package log
|
|
|
|
|
2017-09-15 16:00:00 +00:00
|
|
|
//go:generate autoreadme -f
|
|
|
|
|
2017-08-10 13:35:58 +00:00
|
|
|
import (
|
2017-08-24 08:50:16 +00:00
|
|
|
"fmt"
|
2017-09-01 18:44:50 +00:00
|
|
|
"os"
|
2017-08-24 08:50:16 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2017-09-01 18:44:50 +00:00
|
|
|
// Logger is a wrapper around log.Logger.
|
|
|
|
type Logger struct {
|
|
|
|
log.Logger
|
2017-09-15 16:00:00 +00:00
|
|
|
level log.Lvl
|
|
|
|
handler log.Handler
|
2017-09-01 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// logger is package scope instance of Logger
|
|
|
|
var logger = Logger{
|
|
|
|
Logger: log.New("geth", "StatusIM"),
|
2017-09-15 16:00:00 +00:00
|
|
|
level: log.LvlError,
|
|
|
|
handler: log.StreamHandler(os.Stdout, log.TerminalFormat(true)),
|
2017-09-01 18:44:50 +00:00
|
|
|
}
|
2017-08-24 08:50:16 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-09-15 16:00:00 +00:00
|
|
|
setHandler(logger.level, logger.handler)
|
2017-08-24 08:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel inits status and ethereum-go logging packages,
|
|
|
|
// enabling logging and setting up proper log level.
|
|
|
|
//
|
|
|
|
// Our log levels are in form "DEBUG|ERROR|WARN|etc", while
|
|
|
|
// ethereum-go expects names in lower case: "debug|error|warn|etc".
|
|
|
|
func SetLevel(level string) {
|
2017-09-01 18:44:50 +00:00
|
|
|
lvl := levelFromString(level)
|
|
|
|
|
2017-09-15 16:00:00 +00:00
|
|
|
logger.level = lvl
|
|
|
|
setHandler(lvl, logger.handler)
|
2017-09-01 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogFile configures logger to write output into file.
|
|
|
|
// This call preserves current logging level.
|
|
|
|
func SetLogFile(filename string) error {
|
|
|
|
handler, err := log.FileHandler(filename, log.TerminalFormat(false))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-15 16:00:00 +00:00
|
|
|
logger.handler = handler
|
|
|
|
setHandler(logger.level, handler)
|
2017-09-01 18:44:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func levelFromString(level string) log.Lvl {
|
2017-08-24 08:50:16 +00:00
|
|
|
lvl, err := log.LvlFromString(strings.ToLower(level))
|
|
|
|
if err != nil {
|
2017-09-01 18:44:50 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Incorrect log level: %s, using defaults\n", level)
|
2017-08-24 08:50:16 +00:00
|
|
|
lvl = log.LvlInfo
|
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
return lvl
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 18:44:50 +00:00
|
|
|
// setHandler is a helper that allows log (re)initialization
|
|
|
|
// with different level and handler. Useful for testing.
|
2017-08-24 08:50:16 +00:00
|
|
|
func setHandler(lvl log.Lvl, handler log.Handler) {
|
|
|
|
h := log.LvlFilterHandler(lvl, handler)
|
|
|
|
logger.SetHandler(h)
|
|
|
|
log.Root().SetHandler(h) // ethereum-go logger
|
|
|
|
}
|
2017-08-10 13:35:58 +00:00
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Trace is a package scope alias for logger.Trace
|
2017-08-10 13:35:58 +00:00
|
|
|
func Trace(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Trace(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Debug is a package scope for logger.Debug
|
2017-08-10 13:35:58 +00:00
|
|
|
func Debug(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Debug(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Info is a package scope for logger.Info
|
2017-08-10 13:35:58 +00:00
|
|
|
func Info(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Info(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Warn is a package scope for logger.Warn
|
2017-08-10 13:35:58 +00:00
|
|
|
func Warn(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Warn(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Error is a package scope for logger.Error
|
2017-08-10 13:35:58 +00:00
|
|
|
func Error(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Error(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:50:16 +00:00
|
|
|
// Crit is a package scope for logger.Crit
|
2017-08-10 13:35:58 +00:00
|
|
|
func Crit(msg string, ctx ...interface{}) {
|
2017-08-24 08:50:16 +00:00
|
|
|
logger.Crit(msg, ctx...)
|
2017-08-10 13:35:58 +00:00
|
|
|
}
|