-
Notifications
You must be signed in to change notification settings - Fork 248
/
override.go
100 lines (83 loc) · 2.41 KB
/
override.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package logutils
import (
"os"
"strings"
logging "github.com/ipfs/go-log/v2"
"github.com/ethereum/go-ethereum/log"
)
type LogSettings struct {
Enabled bool
MobileSystem bool
Level string
File string
MaxSize int
MaxBackups int
CompressRotated bool
}
// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib.
func OverrideWithStdLogger(logLevel string) error {
return enableRootLog(logLevel, NewStdHandler(log.TerminalFormat(false)))
}
// OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it.
func OverrideRootLogWithConfig(settings LogSettings, colors bool) error {
if !settings.Enabled {
return nil
}
if settings.MobileSystem {
return OverrideWithStdLogger(settings.Level)
}
return OverrideRootLog(settings.Enabled, settings.Level, FileOptions{
Filename: settings.File,
MaxSize: settings.MaxSize,
MaxBackups: settings.MaxBackups,
Compress: settings.CompressRotated,
}, colors)
}
// OverrideRootLog overrides root logger with file handler, if defined,
// and log level (defaults to INFO).
func OverrideRootLog(enabled bool, levelStr string, fileOpts FileOptions, terminal bool) error {
if !enabled {
disableRootLog()
return nil
}
if os.Getenv("CI") == "true" {
terminal = false
}
var (
handler log.Handler
)
if fileOpts.Filename != "" {
if fileOpts.MaxBackups == 0 {
// Setting MaxBackups to 0 causes all log files to be kept. Even setting MaxAge to > 0 doesn't fix it
// Docs: https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2@v2.0.0#readme-cleaning-up-old-log-files
fileOpts.MaxBackups = 1
}
handler = FileHandlerWithRotation(fileOpts, log.TerminalFormat(terminal))
} else {
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(terminal))
}
return enableRootLog(levelStr, handler)
}
func disableRootLog() {
log.Root().SetHandler(log.DiscardHandler())
}
func enableRootLog(levelStr string, handler log.Handler) error {
if levelStr == "" {
levelStr = "INFO"
}
levelStr = strings.ToLower(levelStr)
level, err := log.LvlFromString(levelStr)
if err != nil {
return err
}
filteredHandler := log.LvlFilterHandler(level, handler)
log.Root().SetHandler(filteredHandler)
log.PrintOrigins(true)
// go-libp2p logger
lvl, err := logging.LevelFromString(levelStr)
if err != nil {
return err
}
logging.SetAllLoggers(lvl)
return nil
}