-
Notifications
You must be signed in to change notification settings - Fork 244
/
override.go
79 lines (66 loc) · 1.87 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
package logutils
import (
"os"
"strings"
"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.LogfmtFormat()))
}
// 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
}
var (
handler log.Handler
)
if fileOpts.Filename != "" {
handler = FileHandlerWithRotation(fileOpts, log.LogfmtFormat())
} 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"
}
level, err := log.LvlFromString(strings.ToLower(levelStr))
if err != nil {
return err
}
filteredHandler := log.LvlFilterHandler(level, handler)
log.Root().SetHandler(filteredHandler)
return nil
}