-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
215 lines (177 loc) · 4.67 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
package log
import (
"context"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type ctxKey int
const logContextKey ctxKey = iota
// Logger ...
type Logger struct {
zapLogger *zap.Logger
options *Options
}
// Create a new logger instance.
func newLogger(l *zap.Logger, o *Options) *Logger {
return &Logger{
zapLogger: l,
options: o,
}
}
// WithName custom logger name.
func (l *Logger) WithName(name string) *Logger {
logger := l.zapLogger.Named(name)
return newLogger(logger, l.options)
}
// WithFields custom other log entry fileds.
func (l *Logger) WithFields(fields ...Field) *Logger {
logger := l.zapLogger.With(fields...)
return newLogger(logger, l.options)
}
// WithHooks is different from SetHooks,
// SetHooks is for global logger,
// WithHooks is for the new logger.
func (l *Logger) WithHooks(hooks ...Hook) *Logger {
logger := l.zapLogger.WithOptions(zap.Hooks(hooks...))
return newLogger(logger, l.options)
}
// ToContext put logger to context.
func (l *Logger) ToContext(ctx context.Context) context.Context {
return context.WithValue(ctx, logContextKey, l)
}
// FromContext return logger from context.
func (l *Logger) FromContext(ctx context.Context) *Logger {
if ctx != nil {
logger := ctx.Value(logContextKey)
if logger != nil {
return logger.(*Logger)
}
}
return l.WithName("UnknownContext")
}
// GetOptions get logger instance configuration.
func (l *Logger) GetOptions() *Options {
return l.options
}
// C get logger from gin.Context.
//
// Usage example:
//
// This is a middleware that put logger into gin.Context:
//
//func Context() gin.HandlerFunc {
//return func(c *gin.Context) {
//l := log.WithFields(
//log.String("x-request-id", c.GetString(XRequestIDKey)),
//log.String("username", c.GetString(UsernameKey)),
//)
//c.Set(log.ContextLoggerName, l)
//c.Next()
//}
//}
//
// Get logger that with fileds from gin.Context:
//
//func (u *UserController) Get(c *gin.Context) {
//log.C(c).Debug("user get called")
//}
//
func (l *Logger) C(ctx context.Context) *Logger {
ctxLogger, ok := ctx.(*gin.Context).Get(ContextLoggerName)
if !ok {
return l
}
cl, ok := ctxLogger.(*Logger)
if !ok {
return l
}
return cl
}
// Debug ...
func (l *Logger) Debug(msg string, fields ...Field) {
l.zapLogger.Debug(msg, fields...)
}
// Debugf ...
func (l *Logger) Debugf(format string, v ...interface{}) {
l.zapLogger.Sugar().Debugf(format, v...)
}
// Debugw ...
func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Debugw(msg, keysAndValues...)
}
// Info ...
func (l *Logger) Info(msg string, fields ...Field) {
l.zapLogger.Info(msg, fields...)
}
// Infof ...
func (l *Logger) Infof(format string, v ...interface{}) {
l.zapLogger.Sugar().Infof(format, v...)
}
// Infow ...
func (l *Logger) Infow(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Infow(msg, keysAndValues...)
}
// Warn ...
func (l *Logger) Warn(msg string, fields ...Field) {
l.zapLogger.Warn(msg, fields...)
}
// Warnf ...
func (l *Logger) Warnf(format string, v ...interface{}) {
l.zapLogger.Sugar().Warnf(format, v...)
}
// Warnw ...
func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Warnw(msg, keysAndValues...)
}
// Error ...
func (l *Logger) Error(msg string, fields ...Field) {
l.zapLogger.Error(msg, fields...)
}
// Errorf ...
func (l *Logger) Errorf(format string, v ...interface{}) {
l.zapLogger.Sugar().Errorf(format, v...)
}
// Errorw ...
func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Errorw(msg, keysAndValues...)
}
// DPanic ...
func (l *Logger) DPanic(msg string, fields ...Field) {
l.zapLogger.DPanic(msg, fields...)
}
// DPanicf ...
func (l *Logger) DPanicf(format string, v ...interface{}) {
l.zapLogger.Sugar().DPanicf(format, v...)
}
// DPanicw ...
func (l *Logger) DPanicw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().DPanicw(msg, keysAndValues...)
}
// Panic ...
func (l *Logger) Panic(msg string, fields ...Field) {
l.zapLogger.Panic(msg, fields...)
}
// Panicf ...
func (l *Logger) Panicf(format string, v ...interface{}) {
l.zapLogger.Sugar().Panicf(format, v...)
}
// Panicw ...
func (l *Logger) Panicw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Panicw(msg, keysAndValues...)
}
// Fatal ...
func (l *Logger) Fatal(msg string, fields ...Field) {
l.zapLogger.Fatal(msg, fields...)
}
// Fatalf ...
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.zapLogger.Sugar().Fatalf(format, v...)
}
// Fatalw ...
func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Fatalw(msg, keysAndValues...)
}
// Sync memory data to log files.
func (l *Logger) Sync() {
_ = l.zapLogger.Sync()
}