-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
280 lines (246 loc) · 8.51 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package logger
import (
"os"
"fmt"
"path"
"time"
"errors"
"runtime"
"path/filepath"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/natefinch/lumberjack"
)
type Log struct {
}
// Configuration for logging
type Config struct {
// EncodeLogsAsJson makes the log framework log JSON
EncodeLogsAsJson bool
// FileLoggingEnabled makes the framework log to a file
// the fields below can be skipped if this value is false!
FileLoggingEnabled bool
// Directory to log to to when filelogging is enabled
Directory string
// Filename is the name of the logfile which will be placed inside the directory
Filename string
// MaxSize the max size in MB of the logfile before it's rolled
MaxSize int
// MaxBackups the max number of rolled files to keep
MaxBackups int
// MaxAge the max age in days to keep a logfile
MaxAge int
// StackStrace make debug log stack
StackStrace bool
// LogLevel log level
LogLevel zapcore.Level
}
// How to log, by example:
// logger.Info("Importing new file, zap.String("source", filename), zap.Int("size", 1024))
// To log a stacktrace:
// logger.Error("It went wrong, zap.Stack())
// DefaultZapLogger is the default logger instance that should be used to log
// It's assigned a default value here for tests (which do not call log.Configure())
var DefaultZapLogger = newZapLogger(false, os.Stdout)
var DefaultLoggerConfig Config
func Bool(name string, value bool) zapcore.Field {
return zap.Bool(name, value)
}
func Int(name string, value int) zapcore.Field {
return zap.Int(name, value)
}
func String(name string, value string) zapcore.Field {
return zap.String(name, value)
}
func Int64(name string, value int64) zapcore.Field {
return zap.Int64(name, value)
}
func Duration(name string, value time.Duration) zapcore.Field {
return zap.Duration(name, value)
}
func Err(err error) zapcore.Field {
return zap.Error(err)
}
// Debug Log a message at the debug level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Debug(msg string, fields ...zapcore.Field) {
if DefaultLoggerConfig.StackStrace {
fields = append(fields, Stack())
DefaultZapLogger.Debug(msg, fields...)
} else {
DefaultZapLogger.Debug(msg, fields...)
}
}
// Info log a message at the info level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Info(msg string, fields ...zapcore.Field) {
DefaultZapLogger.Info(msg, fields...)
}
// Warn log a message at the warn level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Warn(msg string, fields ...zapcore.Field) {
DefaultZapLogger.Warn(msg, fields...)
}
// Error Log a message at the error level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Error(msg string, fields ...zapcore.Field) {
DefaultZapLogger.Error(msg, fields...)
}
// Panic Log a message at the Panic level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Panic(msg string, fields ...zapcore.Field) {
DefaultZapLogger.Panic(msg, fields...)
}
// Fatal Log a message at the fatal level. Messages include any context that's
// accumulated on the logger, as well as any fields added at the log site.
//
// Use zap.String(key, value), zap.Int(key, value) to log fields. These fields
// will be marshalled as JSON in the logfile and key value pairs in the console!
func (l *Log) Fatal(msg string, fields ...zapcore.Field) {
DefaultZapLogger.Fatal(msg, fields...)
}
func Stack() zapcore.Field {
pc, file, lineno, ok := runtime.Caller(2)
src := ""
if ok {
src = fmt.Sprintf("%s:%s:%d", file, runtime.FuncForPC(pc).Name(), lineno)
}
return zap.String("stacktrace", src)
}
//// AtLevel logs the message at a specific log level
//func AtLevel(level zapcore.Level, msg string, fields ...zapcore.Field) {
// switch level {
// case zapcore.DebugLevel:
// Debug(msg, fields...)
// case zapcore.PanicLevel:
// Panic(msg, fields...)
// case zapcore.ErrorLevel:
// Error(msg, fields...)
// case zapcore.WarnLevel:
// Warn(msg, fields...)
// case zapcore.InfoLevel:
// Info(msg, fields...)
// case zapcore.FatalLevel:
// Fatal(msg, fields...)
// default:
// Warn("Logging at unkown level", zap.Any("level", level))
// Warn(msg, fields...)
// }
//}
// Configure sets up the logging framework
//
// In production, the container logs will be collected and file logging should be disabled. However,
// during development it's nicer to see logs as text and optionally write to a file when debugging
// problems in the containerized pipeline
//
// The output log file will be located at /var/log/auth-service/auth-service.log and
// will be rolled when it reaches 20MB with a maximum of 1 backup.
func Configure(config Config) {
writers := []zapcore.WriteSyncer{os.Stdout}
if config.FileLoggingEnabled {
writers = append(writers, newRollingFile(config))
}
DefaultZapLogger = newZapLogger(config.EncodeLogsAsJson, zapcore.NewMultiWriteSyncer(writers...))
zap.RedirectStdLog(DefaultZapLogger)
//Info("logging configured",
// zap.Bool("fileLogging", config.FileLoggingEnabled),
// zap.Bool("jsonLogOutput", config.EncodeLogsAsJson),
// zap.String("logDirectory", config.Directory),
// zap.String("fileName", config.Filename),
// zap.Int("maxSizeMB", config.MaxSize),
// zap.Int("maxBackups", config.MaxBackups),
// zap.Int("maxAgeInDays", config.MaxAge))
DefaultLoggerConfig = config
}
func Init(file, level string, size, backup int, stackstrace bool) (Log, error) {
log := Log{}
name := filepath.Base(file)
if name == "" {
return log, errors.New("Bad file")
}
dir := filepath.Dir(file)
if dir == "" {
dir = "./"
}
if size < 0 || backup < 0 {
return log, errors.New("Bad size or backup")
}
config := Config{
EncodeLogsAsJson: true,
FileLoggingEnabled: true,
Directory: dir,
Filename: name,
MaxSize: size,
MaxBackups: backup,
StackStrace: stackstrace,
}
if level == "" {
level = "error"
}
if err := SetLogLevel(level); err != nil {
return log, err
}
Configure(config)
return log, nil
}
func newRollingFile(config Config) zapcore.WriteSyncer {
if err := os.MkdirAll(config.Directory, 0); err != nil {
fmt.Printf("Failed create log directory in %s, error: %s\n", config.Directory, err)
return nil
}
return zapcore.AddSync(&lumberjack.Logger{
Filename: path.Join(config.Directory, config.Filename),
MaxSize: config.MaxSize, //megabytes
MaxAge: config.MaxAge, //days
MaxBackups: config.MaxBackups, //files
})
}
func newZapLogger(encodeAsJSON bool, output zapcore.WriteSyncer) *zap.Logger {
encCfg := zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.LowercaseLevelEncoder,
//EncodeTime: zapcore.EpochNanosTimeEncoder,
//EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeTime: zapcore.EpochMillisTimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
}
encoder := zapcore.NewConsoleEncoder(encCfg)
if encodeAsJSON {
encoder = zapcore.NewJSONEncoder(encCfg)
}
return zap.New(zapcore.NewCore(encoder, output, zap.NewAtomicLevelAt(DefaultLoggerConfig.LogLevel)))
}
func SetLogLevel(level string) error {
if level == "debug" {
DefaultLoggerConfig.LogLevel = zap.DebugLevel
} else if level == "info" {
DefaultLoggerConfig.LogLevel = zap.InfoLevel
} else if level == "warn" {
DefaultLoggerConfig.LogLevel = zap.WarnLevel
} else if level == "error" {
DefaultLoggerConfig.LogLevel = zap.ErrorLevel
} else {
return errors.New("Bad log level")
}
return nil
}