/
log.go
76 lines (63 loc) · 2.36 KB
/
log.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
package common
import (
"os"
config "github.com/bnb-chain/bsc-relayer/config"
"github.com/op/go-logging"
"github.com/tendermint/tendermint/libs/log"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
// Logger instance for quick declarative logging levels
Logger = logging.MustGetLogger("replayer")
SdkLogger = &sdkLogger{}
// log levels that are available
levels = map[string]logging.Level{
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"NOTICE": logging.NOTICE,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
}
)
// InitLogger initialises the logger.
func InitLogger(config *config.LogConfig) {
backends := make([]logging.Backend, 0)
if config.UseConsoleLogger {
consoleFormat := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05} %{level} %{shortfunc} %{message}`)
consoleLogger := logging.NewLogBackend(os.Stdout, "", 0)
consoleFormatter := logging.NewBackendFormatter(consoleLogger, consoleFormat)
consoleLoggerLeveled := logging.AddModuleLevel(consoleFormatter)
consoleLoggerLeveled.SetLevel(levels[config.Level], "")
backends = append(backends, consoleLoggerLeveled)
}
if config.UseFileLogger {
fileLogger := logging.NewLogBackend(&lumberjack.Logger{
Filename: config.Filename,
MaxSize: config.MaxFileSizeInMB, // MaxSize is the maximum size in megabytes of the log file
MaxBackups: config.MaxBackupsOfLogFiles, // MaxBackups is the maximum number of old log files to retain
MaxAge: config.MaxAgeToRetainLogFilesInDays, // MaxAge is the maximum number of days to retain old log files
Compress: config.Compress,
}, "", 0)
fileFormat := logging.MustStringFormatter(`%{time:2006-01-02 15:04:05} %{level} %{shortfunc} %{message}`)
fileFormatter := logging.NewBackendFormatter(fileLogger, fileFormat)
fileLoggerLeveled := logging.AddModuleLevel(fileFormatter)
fileLoggerLeveled.SetLevel(levels[config.Level], "")
backends = append(backends, fileLoggerLeveled)
}
logging.SetBackend(backends...)
}
type sdkLogger struct {
}
func (l *sdkLogger) Debug(msg string, keyvals ...interface{}) {
Logger.Debug(msg)
}
func (l *sdkLogger) Info(msg string, keyvals ...interface{}) {
Logger.Info(msg)
}
func (l *sdkLogger) Error(msg string, keyvals ...interface{}) {
Logger.Error(msg)
}
func (l *sdkLogger) With(keyvals ...interface{}) log.Logger {
return l
}