/
logger.go
201 lines (168 loc) · 4.61 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
package golog
import (
"fmt"
"runtime"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger 可以看到被调用者的logger
type Logger struct {
isDebug bool
callDeep int
Logger *zap.Logger
field []zapcore.Field
}
func callerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(strings.Join([]string{caller.TrimmedPath(), runtime.FuncForPC(caller.PC).Name()}, ":"))
}
func httpStatusCodeEncoder(code int, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendInt(code)
}
func ios8601Encoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339))
}
func milliSecondsDurationEncoder(d time.Duration, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendFloat64(float64(d) / float64(time.Millisecond))
}
// New new logger
func New(debugLevel bool) (*Logger, error) {
productConfig := zap.NewProductionConfig()
productConfig.EncoderConfig.EncodeTime = ios8601Encoder
productConfig.EncoderConfig.EncodeDuration = milliSecondsDurationEncoder
productConfig.EncoderConfig.EncodeCaller = callerEncoder
if debugLevel {
productConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
l, err := productConfig.Build()
if err != nil {
return nil, err
}
return &Logger{
isDebug: debugLevel,
Logger: l,
}, nil
}
// Caller Add caller field
func (l Logger) Caller(i int) Logger {
newLogger := l
newLogger.Logger.WithOptions(zap.AddCallerSkip(i + 1))
return newLogger
}
// Read output log message
func (l Logger) Read(p []byte) (int, error) {
return 0, nil
}
// func (l Logger) Write(p []byte) (int, error) {
// return 0, nil
// }
// AddField Add new filed to log message
func (l Logger) AddField(key string, value interface{}) {
l.field = append(l.field, zap.Any(key, value))
}
// Print Print
func (l Logger) Print(args ...interface{}) {
l.Logger.Info(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Println Println
func (l Logger) Println(args ...interface{}) {
l.Logger.Info(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Printf Printf
func (l Logger) Printf(format string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Debug Debug
func (l Logger) Debug(args ...interface{}) {
l.Logger.Debug(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Debugln Debugln
func (l Logger) Debugln(args ...interface{}) {
l.Logger.Debug(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Debugf Debugf
func (l Logger) Debugf(format string, args ...interface{}) {
l.Logger.Debug(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Info Info
func (l Logger) Info(args ...interface{}) {
l.Logger.Info(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Infoln Infoln
func (l Logger) Infoln(args ...interface{}) {
l.Logger.Info(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Infof Infof
func (l Logger) Infof(format string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Warn Warn
func (l Logger) Warn(args ...interface{}) {
l.Logger.Warn(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Warnln Warnln
func (l Logger) Warnln(args ...interface{}) {
l.Logger.Warn(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Warnf Warnf
func (l Logger) Warnf(format string, args ...interface{}) {
l.Logger.Warn(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Error Error
func (l Logger) Error(args ...interface{}) {
l.Logger.Error(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Errorln Errorln
func (l Logger) Errorln(args ...interface{}) {
l.Logger.Error(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Errorf Errorf
func (l Logger) Errorf(format string, args ...interface{}) {
l.Logger.Error(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Panic Panic
func (l Logger) Panic(args ...interface{}) {
l.Logger.Panic(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Panicln Panicln
func (l Logger) Panicln(args ...interface{}) {
l.Logger.Panic(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Panicf Panicf
func (l Logger) Panicf(format string, args ...interface{}) {
l.Logger.Panic(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}
// Fatal Fatal
func (l Logger) Fatal(args ...interface{}) {
l.Logger.Fatal(fmt.Sprint(args...), l.field...)
l.Logger.Sync()
}
// Fatalln Fatalln
func (l Logger) Fatalln(args ...interface{}) {
l.Logger.Fatal(fmt.Sprintln(args...), l.field...)
l.Logger.Sync()
}
// Fatalf Fatalf
func (l Logger) Fatalf(format string, args ...interface{}) {
l.Logger.Fatal(fmt.Sprintf(format, args...), l.field...)
l.Logger.Sync()
}