2018-04-22 16:50:34 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2019-03-01 13:37:13 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-22 16:50:34 +00:00
|
|
|
)
|
|
|
|
|
2019-03-05 09:03:06 +00:00
|
|
|
// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib.
|
|
|
|
func OverrideWithStdLogger(config *params.NodeConfig) error {
|
|
|
|
return enableRootLog(config.LogLevel, NewStdHandler(log.LogfmtFormat()))
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:37:13 +00:00
|
|
|
// OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it.
|
|
|
|
func OverrideRootLogWithConfig(config *params.NodeConfig, colors bool) error {
|
2019-03-05 09:03:06 +00:00
|
|
|
if !config.LogEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if config.LogMobileSystem {
|
|
|
|
return OverrideWithStdLogger(config)
|
|
|
|
}
|
2019-03-01 13:37:13 +00:00
|
|
|
return OverrideRootLog(config.LogEnabled, config.LogLevel, FileOptions{
|
|
|
|
Filename: config.LogFile,
|
|
|
|
MaxSize: config.LogMaxSize,
|
|
|
|
MaxBackups: config.LogMaxBackups,
|
|
|
|
Compress: config.LogCompressRotated,
|
|
|
|
}, colors)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
// OverrideRootLog overrides root logger with file handler, if defined,
|
|
|
|
// and log level (defaults to INFO).
|
2019-03-01 13:37:13 +00:00
|
|
|
func OverrideRootLog(enabled bool, levelStr string, fileOpts FileOptions, terminal bool) error {
|
2018-04-26 16:28:42 +00:00
|
|
|
if !enabled {
|
|
|
|
disableRootLog()
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-22 16:50:34 +00:00
|
|
|
var (
|
|
|
|
handler log.Handler
|
|
|
|
)
|
2019-03-01 13:37:13 +00:00
|
|
|
if fileOpts.Filename != "" {
|
|
|
|
handler = FileHandlerWithRotation(fileOpts, log.LogfmtFormat())
|
2018-04-22 16:50:34 +00:00
|
|
|
} else {
|
|
|
|
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(terminal))
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:03:06 +00:00
|
|
|
return enableRootLog(levelStr, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func disableRootLog() {
|
|
|
|
log.Root().SetHandler(log.DiscardHandler())
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableRootLog(levelStr string, handler log.Handler) error {
|
2018-04-22 16:50:34 +00:00
|
|
|
if levelStr == "" {
|
|
|
|
levelStr = "INFO"
|
|
|
|
}
|
|
|
|
|
|
|
|
level, err := log.LvlFromString(strings.ToLower(levelStr))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredHandler := log.LvlFilterHandler(level, handler)
|
|
|
|
log.Root().SetHandler(filteredHandler)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|