-
Notifications
You must be signed in to change notification settings - Fork 211
/
log.go
166 lines (130 loc) · 3.96 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
// Package log provides the both file and console (general) logging capabilities
// to spacemesh modules such as app and identity.
package log
import (
"io"
"os"
"path/filepath"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"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
})
func logLevel() zap.LevelEnablerFunc {
if debugMode {
return debugLevel
}
return infoLevel
}
// Level returns the zapcore level of logging.
func Level() zapcore.Level {
if debugMode {
return zapcore.DebugLevel
}
return zapcore.InfoLevel
}
// Logger is an interface for our logging API.
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
}
// JSONLog sets logging to be in JSON format or not.
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(Level())
return Log{log, log.Sugar(), &lvl}
}
// NewDefault creates a Log with not file output.
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...)
}
// With returns a FieldLogger which you can append fields to.
func With() FieldLogger {
return FieldLogger{AppLog.logger}
}
// Event returns a field logger with the Event field set to true.
func Event() FieldLogger {
return AppLog.Event()
}
// Panic writes the log message and then panics.
func Panic(msg string, args ...interface{}) {
AppLog.Panic(msg, args...)
}