This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
75 lines (61 loc) · 2.27 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
package zlog
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
type MultiLogger map[string]*Setting
func (m MultiLogger) Core() zapcore.Core {
var cores []zapcore.Core
for _, logger := range m {
cores = append(cores, logger.Core())
}
return zapcore.NewTee(cores...)
}
func (s *Setting) Core() zapcore.Core {
c := &config{
format: Json,
encCfg: encoderConfig,
writer: os.Stdout,
level: zapcore.DebugLevel,
}
return c.withSetting(s).Core()
}
type Logger struct {
zapLog *zap.Logger
}
var logger = new(Logger)
func init() {
core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), os.Stdout, zapcore.DebugLevel)
logger.zapLog = zap.New(core).WithOptions(zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(zap.DPanicLevel))
}
func Start(core zapcore.Core, opts ...zap.Option) {
logger.zapLog = zap.New(core, opts...)
}
func Get() *Logger {
return logger
}
func End() {
_ = logger.zapLog.Sync()
}
func Debug(msg string, fields ...zap.Field) { logger.zapLog.Debug(msg, fields...) }
func Info(msg string, fields ...zap.Field) { logger.zapLog.Info(msg, fields...) }
func Warn(msg string, fields ...zap.Field) { logger.zapLog.Warn(msg, fields...) }
func Error(msg string, fields ...zap.Field) { logger.zapLog.Error(msg, fields...) }
func DPanic(msg string, fields ...zap.Field) { logger.zapLog.DPanic(msg, fields...) }
func Panic(msg string, fields ...zap.Field) { logger.zapLog.Panic(msg, fields...) }
func Fatal(msg string, fields ...zap.Field) { logger.zapLog.Fatal(msg, fields...) }
func (l *Logger) Error(msg string) { l.zapLog.Debug(fmt.Sprintf("ERROR: %s", msg)) }
func (l *Logger) Infof(msg string, args ...interface{}) { l.zapLog.Debug(fmt.Sprintf(msg, args...)) }
func (l *Logger) Print(v ...interface{}) { l.zapLog.Debug(fmt.Sprint(v...)) }
func (l *Logger) Printf(format string, v ...interface{}) { l.zapLog.Debug(fmt.Sprintf(format, v...)) }
func (l *Logger) Println(v ...interface{}) { l.zapLog.Debug(fmt.Sprint(v...)) }
func (l *Logger) Write(p []byte) (n int, err error) { l.zapLog.Debug(string(p)); return len(p), nil }
func (l *Logger) Zap() *zap.Logger {
return l.zapLog
}
func (l *Logger) WithOptions(opts ...zap.Option) *Logger {
l.zapLog.WithOptions(opts...)
return l
}