2018-04-22 16:50:34 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-02-01 23:40:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2021-08-18 12:44:10 +00:00
|
|
|
type LogSettings struct {
|
|
|
|
Enabled bool
|
|
|
|
MobileSystem bool
|
|
|
|
Level string
|
|
|
|
File string
|
|
|
|
MaxSize int
|
|
|
|
MaxBackups int
|
|
|
|
CompressRotated bool
|
|
|
|
}
|
|
|
|
|
2019-03-05 09:03:06 +00:00
|
|
|
// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib.
|
2021-08-18 12:44:10 +00:00
|
|
|
func OverrideWithStdLogger(logLevel string) error {
|
|
|
|
return enableRootLog(logLevel, NewStdHandler(log.LogfmtFormat()))
|
2019-03-05 09:03:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 13:37:13 +00:00
|
|
|
// OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it.
|
2021-08-18 12:44:10 +00:00
|
|
|
func OverrideRootLogWithConfig(settings LogSettings, colors bool) error {
|
|
|
|
if !settings.Enabled {
|
2019-03-05 09:03:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-18 12:44:10 +00:00
|
|
|
if settings.MobileSystem {
|
|
|
|
return OverrideWithStdLogger(settings.Level)
|
2019-03-05 09:03:06 +00:00
|
|
|
}
|
2021-08-18 12:44:10 +00:00
|
|
|
return OverrideRootLog(settings.Enabled, settings.Level, FileOptions{
|
|
|
|
Filename: settings.File,
|
|
|
|
MaxSize: settings.MaxSize,
|
|
|
|
MaxBackups: settings.MaxBackups,
|
|
|
|
Compress: settings.CompressRotated,
|
2019-03-01 13:37:13 +00:00
|
|
|
}, 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"
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:40:32 +00:00
|
|
|
levelStr = strings.ToLower(levelStr)
|
|
|
|
|
|
|
|
level, err := log.LvlFromString(levelStr)
|
2018-04-22 16:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredHandler := log.LvlFilterHandler(level, handler)
|
|
|
|
log.Root().SetHandler(filteredHandler)
|
|
|
|
|
2023-02-01 23:40:32 +00:00
|
|
|
// go-libp2p logger
|
|
|
|
lvl, err := logging.LevelFromString(levelStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.SetAllLoggers(lvl)
|
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
return nil
|
|
|
|
}
|