/
logger.go
212 lines (189 loc) · 6.38 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2020 Andy Pan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package logging provides logging functionality for gnet server,
// it sets up a default logger (powered by go.uber.org/zap)
// which is about to be used by gnet server, it also allows users
// to replace the default logger with their customized logger by just
// implementing the `Logger` interface and assign it to the functional option `Options.Logger`,
// pass it to `gnet.Serve` method.
//
// The environment variable `GNET_LOGGING_LEVEL` determines which zap logger level will be applied for logging.
// The environment variable `GNET_LOGGING_FILE` is set to a local file path when you want to print logs into local file.
// Alternatives of logging level (the variable of logging level ought to be integer):
/*
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
)
*/
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.RFC3339NanoTimeEncoder
zapLogger, _ := cfg.Build()
defaultLogger = zapLogger.Sugar()
}
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
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: 2,
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()
}
}
// 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...)
}
// 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{})
}