-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·471 lines (393 loc) · 12.4 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package logadapter
import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
// HeaderKey is key from http Header
type HeaderKey string
// LogKey is key for log messages
type LogKey string
// LogFormat log format
type LogFormat uint32
// Level log level
type Level uint32
// Config config instance log
type Config struct {
IsUseLogFile bool // set true if write to file
FileConfig *FileConfig // ignore if IsUseLogFile = false, set null if use default log file config
LogLevel Level
LogFormat LogFormat
TimestampFormat string // if empty, use default timestamp format
}
// FileConfig config for write log to file
type FileConfig struct {
Filename string
MaxSize int // megabytes
MaxBackups int // number of log files
MaxAge int // days
IsCompress bool
IsUseLocalTime bool
}
// Logger instance
type Logger struct {
*log.Logger
logFormat LogFormat
timestampFormat string
logKeys []LogKey
ignoredPaths []string
}
var l *Logger
func init() {
l = New()
sourceDir()
}
type customFormat struct {
defaultFields map[string]interface{}
formatter log.Formatter
}
func (cl customFormat) Format(entry *log.Entry) ([]byte, error) {
for k, v := range cl.defaultFields {
entry.Data[k] = v
}
return cl.formatter.Format(entry)
}
// SetIgnoredPaths for ignored path in stack trace field.
// By default logadapter print stacktrace with log has lever higher than WARN, always print stack trace with gorm_adapter
// Not to print file path has "runtime/" in stack trace. We use logadapter.UpdateIgnoredPaths([]string{"runtime/"})
func SetIgnoredPaths(paths []string) { l.SetIgnoredPaths(paths) }
// SetIgnoredPaths for ignored path in stack trace field.
// By default logadapter print stacktrace with log has lever higher than WARN, always print stack trace with gorm_adapter
// Not to print file path has "runtime/" in stack trace. We use logadapter.UpdateIgnoredPaths([]string{"runtime/"})
func (l *Logger) SetIgnoredPaths(paths []string) {
l.ignoredPaths = paths
}
// SetFormatter set logger formatter
func SetFormatter(logFormat LogFormat) { l.SetFormatter(logFormat) }
// SetFormatter set logger formatter
func (l *Logger) SetFormatter(logFormat LogFormat) {
switch logFormat {
case JSONFormat:
l.Logger.SetFormatter(&log.JSONFormatter{TimestampFormat: DefaultTimestampFormat})
case PrettyJSONFormat:
l.Logger.SetFormatter(&log.JSONFormatter{PrettyPrint: true, TimestampFormat: DefaultTimestampFormat})
default:
l.Logger.SetFormatter(&log.TextFormatter{TimestampFormat: DefaultTimestampFormat})
}
l.logFormat = logFormat
}
// GetFormatter get logger formatter
func GetFormatter() LogFormat { return l.logFormat }
// SetTimestampFormat set timestamp format
func SetTimestampFormat(timestampFormat string) { l.SetTimestampFormat(timestampFormat) }
// SetTimestampFormat set timestamp format
func (l *Logger) SetTimestampFormat(timestampFormat string) {
switch l.logFormat {
case JSONFormat:
l.Logger.SetFormatter(&log.JSONFormatter{TimestampFormat: timestampFormat})
case PrettyJSONFormat:
l.Logger.SetFormatter(&log.JSONFormatter{PrettyPrint: true, TimestampFormat: timestampFormat})
default:
l.Logger.SetFormatter(&log.TextFormatter{TimestampFormat: timestampFormat})
}
l.timestampFormat = timestampFormat
}
// GetTimestampFormat get timestamp format
func GetTimestampFormat() string { return l.timestampFormat }
// SetDefaultFields set default fields for all log
func SetDefaultFields(fields map[string]interface{}) { l.SetDefaultFields(fields) }
// SetDefaultFields set default fields for all log
func (l *Logger) SetDefaultFields(fields map[string]interface{}) {
if len(fields) > 0 {
l.Logger.SetFormatter(customFormat{
defaultFields: fields,
formatter: l.Formatter,
})
}
}
// SetLogFile set log file, log file will be storaged in logs folder
func SetLogFile() { l.SetLogFile() }
// SetLogFileWithConfig set log file with file config
func (l *Logger) SetLogFileWithConfig(fileConfig *FileConfig) {
if fileConfig == nil {
fileConfig = getDefaultFileConfig()
}
var lumber = &lumberjack.Logger{
Filename: fileConfig.Filename,
MaxSize: fileConfig.MaxSize,
MaxBackups: fileConfig.MaxBackups,
MaxAge: fileConfig.MaxAge,
Compress: fileConfig.IsCompress,
LocalTime: fileConfig.IsUseLocalTime,
}
writer := io.MultiWriter(os.Stdout, lumber)
l.Logger.SetOutput(writer)
}
// SetLogFile set log file, log file will be storaged in logs folder
func (l *Logger) SetLogFile() {
fileConfig := getDefaultFileConfig()
l.SetLogFileWithConfig(fileConfig)
}
// SetLogConsole set log console
func SetLogConsole() { l.SetLogConsole() }
// SetLogConsole set log console
func (l *Logger) SetLogConsole() {
l.SetOutput(os.Stdout)
}
// SetLevel set log level
func SetLevel(level Level) { l.SetLevel(level) }
// SetLevel set log level
func (l *Logger) SetLevel(level Level) {
l.Logger.SetLevel(log.Level(level))
}
// SetLogger set logger instance
func SetLogger(logger *Logger) { l = logger }
// GetLogger set logger instance
func GetLogger() *Logger { return l }
func getDefaultFileConfig() *FileConfig {
return &FileConfig{
Filename: getLogFile(),
MaxSize: 10,
MaxBackups: 3,
MaxAge: 30,
IsCompress: false,
IsUseLocalTime: true,
}
}
func getDefaultConfig() *Config {
return &Config{
LogLevel: DebugLevel,
LogFormat: JSONFormat,
IsUseLogFile: false,
TimestampFormat: DefaultTimestampFormat,
}
}
// RemoveLogKey remove a log key will not log this key
func (l *Logger) RemoveLogKey(key string) {
for i := 0; i < len(l.logKeys); i++ {
if strings.EqualFold(string(l.logKeys[i]), key) {
l.logKeys = append(l.logKeys[:i], l.logKeys[i+1:]...)
}
}
}
// RemoveLogKey export remove a log key will not log this key
func RemoveLogKey(key string) { l.RemoveLogKey(key) }
// addLogKey add one more log key
func (l *Logger) addLogKey(key string) {
if !logKeyExists(l.logKeys, LogKey(key)) {
l.logKeys = append(l.logKeys, LogKey(key))
}
}
// SetContext set log with context
func (l *Logger) SetContext(ctx context.Context) *log.Entry {
return l.WithContext(ctx).WithFields(l.GetLogFieldFromContext(ctx))
}
// SetContext set log with context
func SetContext(ctx context.Context) *log.Entry {
return l.SetContext(ctx)
}
// GetLogFieldFromContext gets log field from context for log field
func GetLogFieldFromContext(ctx context.Context) map[string]interface{} {
return l.GetLogFieldFromContext(ctx)
}
// GetLogFieldFromContext gets log field from context for log field
func (l *Logger) GetLogFieldFromContext(ctx context.Context) map[string]interface{} {
fields := make(map[string]interface{})
for _, key := range l.logKeys {
val := getContextKeyValue(ctx, string(key))
if val != nil {
fields[string(key)] = val
}
}
return fields
}
// SetCustomLogField set custom log field for always log this field, return new context
func (l *Logger) SetCustomLogField(ctx context.Context, logKey string, value interface{}) context.Context {
l.addLogKey(logKey)
return setContextKeyValue(ctx, logKey, value)
}
// SetCustomLogField set custom log field for always log this field, return new context
func SetCustomLogField(ctx context.Context, logKey string, value interface{}) context.Context {
return l.SetCustomLogField(ctx, logKey, value)
}
func (l *Logger) getCaller() string {
var caller string
pc := make([]uintptr, 10)
n := runtime.Callers(2, pc)
if n == 0 {
return caller
}
pc = pc[:n]
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
if strings.Contains(frame.File, baseSourceDir) {
continue
}
var isIgnore bool
for _, path := range l.ignoredPaths {
if strings.Contains(frame.File, path) {
isIgnore = true
continue
}
}
if isIgnore {
continue
}
if len(frame.Function) > 0 && len(frame.File) > 0 {
if len(caller) > 0 {
caller += "\n"
}
caller += fmt.Sprintf("%s\n\t%s:%d", getFunctionName(frame.Function), frame.File, frame.Line)
}
if !more {
break
}
}
return caller
}
// NewWithConfig returns a logger instance with custom configuration
func NewWithConfig(config *Config) *Logger {
if config == nil {
config = getDefaultConfig()
}
logger := log.New()
l := &Logger{Logger: logger}
l.logFormat = config.LogFormat
l.SetFormatter(config.LogFormat)
if len(config.TimestampFormat) > 0 {
l.SetTimestampFormat(config.TimestampFormat)
}
if config.IsUseLogFile == true {
l.SetLogFileWithConfig(config.FileConfig)
} else {
l.SetLogConsole()
}
l.SetLevel(config.LogLevel)
l.logKeys = DefaultLogKeys
return l
}
// New returns a logger instance with default configuration
func New() *Logger {
config := getDefaultConfig()
logger := log.New()
l := &Logger{Logger: logger}
l.SetFormatter(config.LogFormat)
l.logFormat = config.LogFormat
if len(config.TimestampFormat) > 0 {
l.SetTimestampFormat(config.TimestampFormat)
}
if config.IsUseLogFile == true {
l.SetLogFileWithConfig(config.FileConfig)
} else {
l.SetLogConsole()
}
l.SetLevel(config.LogLevel)
l.logKeys = DefaultLogKeys
return l
}
// Trace log with trace level
func Trace(args ...interface{}) {
l.Trace(args...)
}
// Debug log with debug level
func Debug(args ...interface{}) {
l.Debug(args...)
}
// Info log with info level
func Info(args ...interface{}) {
l.Info(args...)
}
// Warn log with warn level
func Warn(args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.WithFields(field).Warn(args...)
}
// Error log with error level
func Error(args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.WithFields(field).Error(args...)
}
// Fatal log with fatal level
func Fatal(args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.WithFields(field).Fatal(args...)
}
// Panic log with panic level
func Panic(args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.WithFields(field).Panic(args...)
}
// TraceWithContext log with trace level
func TraceWithContext(ctx context.Context, args ...interface{}) {
l.SetContext(ctx).Trace(args...)
}
// DebugWithContext log with debug level
func DebugWithContext(ctx context.Context, args ...interface{}) {
l.SetContext(ctx).Debug(args...)
}
// InfoWithContext log with info level
func InfoWithContext(ctx context.Context, args ...interface{}) {
l.SetContext(ctx).Info(args...)
}
// WarnWithContext log with warn level
func WarnWithContext(ctx context.Context, args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.SetContext(ctx).WithFields(field).Warn(args...)
}
// ErrorWithContext log with error level
func ErrorWithContext(ctx context.Context, args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.SetContext(ctx).WithFields(field).Error(args...)
}
// FatalWithContext log with fatal level
func FatalWithContext(ctx context.Context, args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.SetContext(ctx).WithFields(field).Fatal(args...)
}
// PanicWithContext log with panic level
func PanicWithContext(ctx context.Context, args ...interface{}) {
field := log.Fields{DefaultSourceField: l.getCaller()}
l.SetContext(ctx).WithFields(field).Panic(args...)
}
// LogWithContext log content with context
// content[0] : message -> interface{},
// content[1] : log type -> string,
// content[2] : log field -> map[string]interface{}
func LogWithContext(ctx context.Context, content ...interface{}) {
var logType string
if len(content) > 1 {
if value, ok := content[1].(string); ok && value != "" {
logType = value
} else {
logType = LogTypeDebug
}
}
logFields := mergeLogFields(GetLogFieldFromContext(ctx), map[string]interface{}{"type": logType})
if len(content) > 2 {
if maps, ok := content[2].(map[string]interface{}); ok {
logFields = mergeLogFields(logFields, maps)
}
}
switch logType {
case LogTypeAPI:
l.Logger.WithFields(logFields).Info(content[0])
case LogTypeError:
field := log.Fields{DefaultSourceField: l.getCaller()}
l.Logger.WithFields(mergeLogFields(logFields, field)).Error(content[0])
case LogTypeInfo:
l.Logger.WithFields(logFields).Info(content[0])
case LogTypeWarn:
field := log.Fields{DefaultSourceField: l.getCaller()}
l.Logger.WithFields(mergeLogFields(logFields, field)).Warn(content[0])
case LogTypeRequest, LogTypeResponse:
l.Logger.WithFields(logFields).Info(content[0])
default:
l.Logger.WithFields(logFields).Debug(content[0])
}
}