-
Notifications
You must be signed in to change notification settings - Fork 211
/
log.go
186 lines (145 loc) · 4.38 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package log provides the both file and console (general) logging capabilities
// to spacemesh modules such as app and identity.
package log
import (
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
"path/filepath"
"go.uber.org/zap"
)
const mainLoggerName = "00000.defaultLogger"
// determine the level of messages we show.
var debugMode = false
// should we format out logs in json
var jsonLog = false
var DebugLevel = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.DebugLevel
})
var InfoLevel = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.InfoLevel
})
var ErrorLevel = zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
func logLevel() zap.LevelEnablerFunc {
if debugMode {
return DebugLevel
} else {
return InfoLevel
}
}
func LogLvl() zapcore.Level {
if debugMode {
return zapcore.DebugLevel
} else {
return zapcore.InfoLevel
}
}
type Logger interface {
Info(format string, args ...interface{})
Debug(format string, args ...interface{})
Error(format string, args ...interface{})
Warning(format string, args ...interface{})
WithName(prefix string) Log
}
func encoder() zapcore.Encoder {
if jsonLog {
return zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig())
}
return zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
}
// AppLog is the local app singleton logger.
var AppLog Log
func init() {
// create a basic temp os.Stdout logger
// This logger is used until the app calls InitSpacemeshLoggingSystem().
AppLog = NewDefault(mainLoggerName)
}
// DebugMode sets log debug level
func DebugMode(mode bool) {
debugMode = mode
}
func JSONLog(b bool) {
jsonLog = b
}
// New creates a logger for a module. e.g. p2p instance logger.
func New(module string, dataFolderPath string, logFileName string) Log {
var cores []zapcore.Core
consoleSyncer := zapcore.AddSync(os.Stdout)
enc := encoder()
cores = append(cores, zapcore.NewCore(enc, consoleSyncer, logLevel()))
if dataFolderPath != "" && logFileName != "" {
wr := getFileWriter(dataFolderPath, logFileName)
fs := zapcore.AddSync(wr)
cores = append(cores, zapcore.NewCore(enc, fs, DebugLevel))
}
core := zapcore.NewTee(cores...)
log := zap.New(core)
log = log.Named(module)
lvl := zap.NewAtomicLevelAt(LogLvl())
return Log{log, log.Sugar(), &lvl}
}
// New creates a logger for a module. e.g. p2p instance logger.
func NewWithErrorLevel(module string, dataFolderPath string, logFileName string) Log {
var cores []zapcore.Core
consoleSyncer := zapcore.AddSync(os.Stdout)
enc := encoder()
cores = append(cores, zapcore.NewCore(enc, consoleSyncer, ErrorLevel))
if dataFolderPath != "" && logFileName != "" {
wr := getFileWriter(dataFolderPath, logFileName)
fs := zapcore.AddSync(wr)
cores = append(cores, zapcore.NewCore(enc, fs, DebugLevel))
}
core := zapcore.NewTee(cores...)
log := zap.New(core)
log = log.Named(module)
lvl := zap.NewAtomicLevelAt(zap.ErrorLevel)
return Log{log, log.Sugar(), &lvl}
}
func NewDefault(module string) Log {
return New(module, "", "")
}
// getBackendLevelWithFileBackend returns backends level including log file backend
func getFileWriter(dataFolderPath, logFileName string) io.Writer {
fileName := filepath.Join(dataFolderPath, logFileName)
fileLogger := &lumberjack.Logger{
Filename: fileName,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
Compress: false,
}
return fileLogger
}
// InitSpacemeshLoggingSystem initializes app logging system.
func InitSpacemeshLoggingSystem(dataFolderPath string, logFileName string) {
AppLog = NewDefault(mainLoggerName)
}
// public wrappers abstracting away logging lib impl
// Info prints formatted info level log message.
func Info(msg string, args ...interface{}) {
AppLog.Info(msg, args...)
}
// Debug prints formatted debug level log message.
func Debug(msg string, args ...interface{}) {
AppLog.Debug(msg, args...)
}
// Error prints formatted error level log message.
func Error(msg string, args ...interface{}) {
AppLog.Error(msg, args...)
}
// Warning prints formatted warning level log message.
func Warning(msg string, args ...interface{}) {
AppLog.Warning(msg, args...)
}
func With() fieldLogger {
return fieldLogger{AppLog.logger}
}
func Event() fieldLogger {
return AppLog.Event()
}
func Panic(msg string, args ...interface{}) {
AppLog.Panic(msg, args...)
}