/
log.go
231 lines (194 loc) · 10.1 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
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
package log
import (
"context"
"fmt"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
)
var logger = NewLogger(Config{Level: DebugLevel})
// SetLoggerConfig sets the logging config.
func SetLoggerConfig(config Config) {
logger = NewLogger(config)
}
// SetLoggerLevel sets the logging level.
func SetLoggerLevel(level zapcore.Level) {
SetLoggerConfig(Config{Level: level})
}
// Instantiate a new logger and assign any key-value pair to addedInfo field in logger to log additional
// information specific to service
func GetNewLoggerWithDefaultFields(addedInfo ...interface{}) *Logger {
return NewLoggerWithDefaultFields(Config{Level: DebugLevel}, addedInfo)
}
// Debug logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
Debugv(ctx, 1, msg, keysAndValues...)
}
// Debugf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Debugf(ctx context.Context, fs string, args ...interface{}) {
Debugv(ctx, 1, fmt.Sprintf(fs, args...))
}
// Debugv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) {
logger.Debugv(ctx, skip+1, msg, keysAndValues...)
}
// DebugNoCtx logs a message and variadic key-value pairs.
func DebugNoCtx(msg string, keysAndValues ...interface{}) {
// context.Background() is a singleton and gets initialized once
Debugv(context.Background(), 1, msg, keysAndValues...)
}
// DebugfNoCtx uses fmt.Sprintf to log a templated message.
func DebugfNoCtx(fs string, args ...interface{}) {
Debugv(context.Background(), 1, fmt.Sprintf(fs, args...))
}
// Info logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
Infov(ctx, 1, msg, keysAndValues...)
}
// Infof uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Infof(ctx context.Context, fs string, args ...interface{}) {
Infov(ctx, 1, fmt.Sprintf(fs, args...))
}
// Infov logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) {
logger.Infov(ctx, skip+1, msg, keysAndValues...)
}
// InfoNoCtx logs a message and variadic key-value pairs.
func InfoNoCtx(msg string, keysAndValues ...interface{}) {
// context.Background() is a singleton and gets initialized once
Infov(context.Background(), 1, msg, keysAndValues...)
}
// InfofNoCtx uses fmt.Sprintf to log a templated message.
func InfofNoCtx(fs string, args ...interface{}) {
Infov(context.Background(), 1, fmt.Sprintf(fs, args...))
}
// Warn logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Warn(ctx context.Context, msg string, keysAndValues ...interface{}) {
Warnv(ctx, 1, msg, keysAndValues...)
}
// Warnf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Warnf(ctx context.Context, fs string, args ...interface{}) {
Warnv(ctx, 1, fmt.Sprintf(fs, args...))
}
// Warnv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) {
logger.Warnv(ctx, skip+1, msg, keysAndValues...)
}
// WarnNoCtx logs a message and variadic key-value pairs.
func WarnNoCtx(msg string, keysAndValues ...interface{}) {
// context.Background() is a singleton and gets initialized once
Warnv(context.Background(), 1, msg, keysAndValues...)
}
// WarnfNoCtx uses fmt.Sprintf to log a templated message.
func WarnfNoCtx(fs string, args ...interface{}) {
Warnv(context.Background(), 1, fmt.Sprintf(fs, args...))
}
// Fatal logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Fatal(ctx context.Context, msg string, keysAndValues ...interface{}) {
Fatalv(ctx, 1, msg, keysAndValues...)
}
// Fatalf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Fatalf(ctx context.Context, fs string, args ...interface{}) {
Fatalv(ctx, 1, fmt.Sprintf(fs, args...))
}
// Fatalv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) {
logger.Fatalv(ctx, skip+1, msg, keysAndValues...)
}
// FatalNoCtx logs a message and variadic key-value pairs.
func FatalNoCtx(msg string, keysAndValues ...interface{}) {
// context.Background() is a singleton and gets initialized once
Fatalv(context.Background(), 1, msg, keysAndValues...)
}
// FatalfNoCtx uses fmt.Sprintf to log a templated message.
func FatalfNoCtx(fs string, args ...interface{}) {
Fatalv(context.Background(), 1, fmt.Sprintf(fs, args...))
}
// Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Error(ctx context.Context, msg string, keysAndValues ...interface{}) {
Errorv(ctx, 1, msg, keysAndValues...)
}
// Errorf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Errorf(ctx context.Context, fs string, args ...interface{}) {
Errorv(ctx, 1, fmt.Sprintf(fs, args...))
}
// Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
func Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) {
logger.Errorv(ctx, skip+1, msg, keysAndValues...)
}
// ErrorNoCtx logs a message and variadic key-value pairs.
func ErrorNoCtx(msg string, keysAndValues ...interface{}) {
// context.Background() is a singleton and gets initialized once
Errorv(context.Background(), 1, msg, keysAndValues...)
}
// ErrorfNoCtx uses fmt.Sprintf to log a templated message.
func ErrorfNoCtx(fs string, args ...interface{}) {
Errorv(context.Background(), 1, fmt.Sprintf(fs, args...))
}
// Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
// Returns an error with the given message for convenience
func ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) error {
return ErrorvAndReturn(ctx, 1, msg, keysAndValues...)
}
// Errorf uses fmt.Errorf to construct an error and log its message
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
// Returns the error for convenicence
func ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error {
err := fmt.Errorf(fs, args...)
Errorv(ctx, 1, err.Error())
return err
}
// Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs.
// Caller is skipped by skip.
// If ctx is nil, all fields from contextFields will be omitted.
// If ctx does not contain a key in contextFields, that field will be omitted.
// Returns an error with the given message for convenience
func ErrorvAndReturn(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) error {
return logger.ErrorvAndReturn(ctx, skip+1, msg, keysAndValues...)
}
// ErrorNoCtx logs a message and variadic key-value pairs.
// Returns an error with the given message for convenience
func ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error {
// context.Background() is a singleton and gets initialized once
return ErrorvAndReturn(context.Background(), 1, msg, keysAndValues...)
}
func InjectTestLogger(testLogger *zap.SugaredLogger) {
logger = NewLoggerFromCore(testLogger)
}