-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.go
166 lines (144 loc) · 4.48 KB
/
logger.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 logging
import (
"errors"
"os"
"strconv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
flushLogs func() error
defaultLogger Logger
defaultLoggingLevel Level
)
// Level is the alias of zapcore.Level.
type Level = zapcore.Level
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel
// PanicLevel logs a message, then panics.
PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel
)
func init() {
lvl := os.Getenv("GNET_LOGGING_LEVEL")
if len(lvl) > 0 {
loggingLevel, err := strconv.ParseInt(lvl, 10, 8)
if err != nil {
panic("invalid GNET_LOGGING_LEVEL, " + err.Error())
}
defaultLoggingLevel = Level(loggingLevel)
}
// Initializes the inside default logger of gnet.
fileName := os.Getenv("GNET_LOGGING_FILE")
if len(fileName) > 0 {
var err error
defaultLogger, flushLogs, err = CreateLoggerAsLocalFile(fileName, defaultLoggingLevel)
if err != nil {
panic("invalid GNET_LOGGING_FILE, " + err.Error())
}
} else {
cfg := zap.NewDevelopmentConfig()
cfg.Level = zap.NewAtomicLevelAt(defaultLoggingLevel)
cfg.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02T15:04:05.000")
zapLogger, _ := cfg.Build()
defaultLogger = zapLogger.Sugar()
}
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02T15:04:05.000")
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}
// GetDefaultLogger returns the default logger.
func GetDefaultLogger() Logger {
return defaultLogger
}
// LogLevel tells what the default logging level is.
func LogLevel() string {
return defaultLoggingLevel.String()
}
// CreateLoggerAsLocalFile setups the logger by local file path.
func CreateLoggerAsLocalFile(localFilePath string, logLevel Level) (logger Logger, flush func() error, err error) {
if len(localFilePath) == 0 {
return nil, nil, errors.New("invalid local logger path")
}
// lumberjack.Logger is already safe for concurrent use, so we don't need to lock it.
lumberJackLogger := &lumberjack.Logger{
Filename: localFilePath,
MaxSize: 100, // megabytes
MaxBackups: 100,
MaxAge: 15, // days
}
encoder := getEncoder()
ws := zapcore.AddSync(lumberJackLogger)
zapcore.Lock(ws)
levelEnabler := zap.LevelEnablerFunc(func(level Level) bool {
return level >= logLevel
})
core := zapcore.NewCore(encoder, ws, levelEnabler)
zapLogger := zap.New(core, zap.AddCaller())
logger = zapLogger.Sugar()
flush = zapLogger.Sync
return
}
// Cleanup does something windup for logger, like closing, flushing, etc.
func Cleanup() {
if flushLogs != nil {
_ = flushLogs()
}
}
// Logger is used for logging formatted messages.
type Logger interface {
// Debugf logs messages at DEBUG level.
Debugf(format string, args ...interface{})
// Infof logs messages at INFO level.
Infof(format string, args ...interface{})
// Warnf logs messages at WARN level.
Warnf(format string, args ...interface{})
// Errorf logs messages at ERROR level.
Errorf(format string, args ...interface{})
// Fatalf logs messages at FATAL level.
Fatalf(format string, args ...interface{})
}
// Error prints err if it's not nil.
func Error(err error) {
if err != nil {
defaultLogger.Errorf("error occurs during runtime, %v", err)
}
}
// Debugf logs messages at DEBUG level.
func Debugf(format string, args ...interface{}) {
defaultLogger.Debugf(format, args...)
}
// Infof logs messages at INFO level.
func Infof(format string, args ...interface{}) {
defaultLogger.Infof(format, args...)
}
// Warnf logs messages at WARN level.
func Warnf(format string, args ...interface{}) {
defaultLogger.Warnf(format, args...)
}
// Errorf logs messages at ERROR level.
func Errorf(format string, args ...interface{}) {
defaultLogger.Errorf(format, args...)
}
// Fatalf logs messages at FATAL level.
func Fatalf(format string, args ...interface{}) {
defaultLogger.Fatalf(format, args...)
}