-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
217 lines (190 loc) · 5.6 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
package logger
import (
"context"
"fmt"
"io"
"os"
"reflect"
"strings"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/thoohv5/person/pkg/log"
"github.com/thoohv5/person/internal/util"
)
var _ log.Logger = (*defaultLogger)(nil)
type defaultLogger struct {
log *zap.Logger
ctx context.Context
}
const (
OutSep = ","
)
func New(loggerConf *Config) (log.Logger, func(), error) {
var zlg *zap.Logger
if loggerConf.Model == 1 {
zlg = zap.New(getWriter(loggerConf))
} else {
zlg = zap.New(getWriter(loggerConf), zap.AddCaller(), zap.Development(), zap.AddCallerSkip(1))
}
zlg = zlg.WithOptions(zap.AddStacktrace(zap.ErrorLevel))
return &defaultLogger{log: zlg}, func() {
}, nil
}
func pre(ctx context.Context, fields ...log.Field) []zap.Field {
fs := NewFields()
FieldContext(ctx)(fs)
for _, field := range fields {
field(fs)
}
items := make([]zap.Field, 0, len(fs.Data()))
log.SMap(fs.Data(), func(key string, item *log.Entity) {
switch item.Type {
case reflect.String:
val, ok := item.Value.(string)
if !ok {
val = fmt.Sprint(val)
}
items = append(items, zap.String(key, val))
default:
items = append(items, zap.Any(key, item.Value))
}
})
return items
}
func (l *defaultLogger) AddCallerSkip(skip int) log.Logger {
copy := *l
copy.log = copy.log.WithOptions(zap.AddCallerSkip(skip))
return ©
}
func (l *defaultLogger) Debugc(ctx context.Context, msg string, fields ...log.Field) {
l.log.Debug(msg, pre(ctx, fields...)...)
}
func (l *defaultLogger) Infoc(ctx context.Context, msg string, fields ...log.Field) {
l.log.Info(msg, pre(ctx, fields...)...)
}
func (l *defaultLogger) Warnc(ctx context.Context, msg string, fields ...log.Field) {
l.log.Warn(msg, pre(ctx, fields...)...)
}
func (l *defaultLogger) Errorc(ctx context.Context, msg string, fields ...log.Field) {
l.log.Error(msg, pre(ctx, fields...)...)
}
func (l *defaultLogger) Debugf(ctx context.Context, msg string, values ...interface{}) {
l.log.Debug(fmt.Sprintf(msg, values...), pre(ctx)...)
}
func (l *defaultLogger) Infof(ctx context.Context, msg string, values ...interface{}) {
l.log.Info(fmt.Sprintf(msg, values...), pre(ctx)...)
}
func (l *defaultLogger) Warnf(ctx context.Context, msg string, values ...interface{}) {
l.log.Warn(fmt.Sprintf(msg, values...), pre(ctx)...)
}
func (l *defaultLogger) Errorf(ctx context.Context, msg string, values ...interface{}) {
l.log.Error(fmt.Sprintf(msg, values...), pre(ctx)...)
}
func (l *defaultLogger) Sync() error {
// return l.log.Sync()
return nil
}
func (l *defaultLogger) Close() error {
return l.Sync()
}
type devNullLog struct {
w io.Writer
}
func NewDevNullLog(w io.Writer) log.Logger {
return &devNullLog{
w: w,
}
}
func (d *devNullLog) AddCallerSkip(skip int) log.Logger {
return d
}
func (d *devNullLog) Debugc(ctx context.Context, msg string, fields ...log.Field) {
fmt.Fprintf(d.w, msg, pre(ctx, fields...))
}
func (d *devNullLog) Infoc(ctx context.Context, msg string, fields ...log.Field) {
fmt.Fprintf(d.w, msg, pre(ctx, fields...))
}
func (d *devNullLog) Warnc(ctx context.Context, msg string, fields ...log.Field) {
fmt.Fprintf(d.w, msg, pre(ctx, fields...))
}
func (d *devNullLog) Errorc(ctx context.Context, msg string, fields ...log.Field) {
fmt.Fprintf(d.w, msg, pre(ctx, fields...))
}
func (d *devNullLog) Debugf(ctx context.Context, msg string, values ...interface{}) {
fmt.Fprintf(d.w, msg, values...)
}
func (d *devNullLog) Infof(ctx context.Context, msg string, values ...interface{}) {
fmt.Fprintf(d.w, msg, values...)
}
func (d *devNullLog) Warnf(ctx context.Context, msg string, values ...interface{}) {
fmt.Fprintf(d.w, msg, values...)
}
func (d *devNullLog) Errorf(ctx context.Context, msg string, values ...interface{}) {
fmt.Fprintf(d.w, msg, values...)
}
func getWriter(conf *Config) zapcore.Core {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "log",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
FunctionKey: "func",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
}
ws := make([]zapcore.WriteSyncer, 0)
for _, out := range strings.Split(conf.GetOut(), OutSep) {
switch out {
case "std":
ws = append(ws, zapcore.AddSync(os.Stdout))
case "discard":
ws = append(ws, zapcore.AddSync(io.Discard))
default:
ws = append(ws, zapcore.AddSync(getFileWriter(conf.GetFile())))
}
}
var encoder zapcore.Encoder
if conf.Type == "text" {
encoder = NewTextEncoder(encoderConfig)
} else {
encoder = zapcore.NewJSONEncoder(encoderConfig)
}
return zapcore.NewCore(
encoder,
zapcore.NewMultiWriteSyncer(ws...),
zap.NewAtomicLevelAt(parseLevel(conf.GetLevel())),
)
}
// 日志类别: debug, warn, info,error
func parseLevel(level string) zapcore.Level {
zl := zapcore.DebugLevel
switch level {
case "debug":
zl = zapcore.DebugLevel
case "warn":
zl = zapcore.WarnLevel
case "info":
zl = zapcore.InfoLevel
case "error":
zl = zapcore.ErrorLevel
}
return zl
}
func getFileWriter(fc *File) io.Writer {
if strings.HasPrefix(fc.GetPath(), ".") {
fc.Path = util.AbPath(fc.GetPath())
}
return &lumberjack.Logger{
Filename: fmt.Sprintf("%s/%s", fc.GetPath(), fc.GetFileName()),
MaxSize: int(fc.GetMaxSize()),
MaxBackups: int(fc.GetMaxBackups()),
MaxAge: int(fc.GetMaxAge()),
Compress: fc.GetCompress(),
}
}