forked from pingcap/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
247 lines (218 loc) · 8.54 KB
/
global.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"context"
"fmt"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ctxLogKeyType struct{}
var CtxLogKey = ctxLogKeyType{}
//// Debug logs a message at DebugLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
//func Debug(msg string, fields ...zap.Field) {
// L().Debug(msg, fields...)
//}
//// Info logs a message at InfoLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
//func Info(msg string, fields ...zap.Field) {
// L().Info(msg, fields...)
//}
//// Warn logs a message at WarnLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
//func Warn(msg string, fields ...zap.Field) {
// L().Warn(msg, fields...)
//}
//// Error logs a message at ErrorLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
//func Error(msg string, fields ...zap.Field) {
// L().Error(msg, fields...)
//}
//// Panic logs a message at PanicLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
////
//// The logger then panics, even if logging at PanicLevel is disabled.
//func Panic(msg string, fields ...zap.Field) {
// L().Panic(msg, fields...)
//}
//// Fatal logs a message at FatalLevel. The message includes any fields passed
//// at the log site, as well as any fields accumulated on the logger.
////
//// The logger then calls os.Exit(1), even if logging at FatalLevel is
//// disabled.
//func Fatal(msg string, fields ...zap.Field) {
// L().Fatal(msg, fields...)
//}
// RatedDebug print logs at debug level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedDebug(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Debug(msg, fields...)
return true
}
return false
}
// RatedInfo print logs at info level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedInfo(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Info(msg, fields...)
return true
}
return false
}
// RatedWarn print logs at warn level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedWarn(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Warn(msg, fields...)
return true
}
return false
}
// With creates a child logger and adds structured context to it.
// Fields added to the child don't affect the parent, and vice versa.
func With(fields ...zap.Field) *MLogger {
return &MLogger{
Logger: L().With(fields...).WithOptions(zap.AddCallerSkip(-1)),
}
}
// SetLevel alters the logging level.
func SetLevel(l zapcore.Level) {
_globalP.Load().(*ZapProperties).Level.SetLevel(l)
}
// GetLevel gets the logging level.
func GetLevel() zapcore.Level {
return _globalP.Load().(*ZapProperties).Level.Level()
}
// WithTraceID returns a context with trace_id attached
func WithTraceID(ctx context.Context, traceID string) context.Context {
return WithFields(ctx, zap.String("traceID", traceID))
}
// WithReqID adds given reqID field to the logger in ctx
func WithReqID(ctx context.Context, reqID int64) context.Context {
fields := []zap.Field{zap.Int64("reqID", reqID)}
return WithFields(ctx, fields...)
}
// WithModule adds given module field to the logger in ctx
func WithModule(ctx context.Context, module string) context.Context {
fields := []zap.Field{zap.String("module", module)}
return WithFields(ctx, fields...)
}
// WithFields returns a context with fields attached
func WithFields(ctx context.Context, fields ...zap.Field) context.Context {
var zlogger *zap.Logger
if ctxLogger, ok := ctx.Value(CtxLogKey).(*MLogger); ok {
zlogger = ctxLogger.Logger
} else {
zlogger = ctxL()
}
mLogger := &MLogger{
Logger: zlogger.With(fields...),
}
return context.WithValue(ctx, CtxLogKey, mLogger)
}
// Ctx returns a logger which will log contextual messages attached in ctx
func Ctx(ctx context.Context) *MLogger {
if ctx == nil {
return &MLogger{Logger: ctxL()}
}
if ctxLogger, ok := ctx.Value(CtxLogKey).(*MLogger); ok {
return ctxLogger
}
return &MLogger{Logger: ctxL()}
}
// withLogLevel returns ctx with a leveled logger, notes that it will overwrite logger previous attached!
func withLogLevel(ctx context.Context, level zapcore.Level) context.Context {
var zlogger *zap.Logger
switch level {
case zap.DebugLevel:
zlogger = debugL()
case zap.InfoLevel:
zlogger = infoL()
case zap.WarnLevel:
zlogger = warnL()
case zap.ErrorLevel:
zlogger = errorL()
case zap.FatalLevel:
zlogger = fatalL()
default:
zlogger = L()
}
return context.WithValue(ctx, CtxLogKey, &MLogger{Logger: zlogger})
}
// WithDebugLevel returns context with a debug level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithDebugLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.DebugLevel)
}
// WithInfoLevel returns context with a info level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithInfoLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.InfoLevel)
}
// WithWarnLevel returns context with a warning level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithWarnLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.WarnLevel)
}
// WithErrorLevel returns context with a error level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithErrorLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.ErrorLevel)
}
// WithFatalLevel returns context with a fatal level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithFatalLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.FatalLevel)
}
func Print(args ...interface{}) { S().Info(args...) }
func Println(args ...interface{}) {
message := fmt.Sprintln(args...)
message = strings.TrimSuffix(message, "\n")
S().Info(message)
}
func Printf(template string, args ...interface{}) { S().Infof(template, args...) }
func Debug(args ...interface{}) { S().Debug(args...) }
func Debugf(template string, args ...interface{}) { S().Debugf(template, args...) }
func Debugw(msg string, keysAndValues ...interface{}) { S().Debugw(msg, keysAndValues...) }
func Info(args ...interface{}) { S().Info(args...) }
func Infof(template string, args ...interface{}) { S().Infof(template, args...) }
func Infow(msg string, keysAndValues ...interface{}) { S().Infow(msg, keysAndValues...) }
func Warn(args ...interface{}) { S().Warn(args...) }
func Warnf(template string, args ...interface{}) { S().Warnf(template, args...) }
func Warnw(msg string, keysAndValues ...interface{}) { S().Warnw(msg, keysAndValues...) }
func Error(args ...interface{}) { S().Error(args...) }
func Errorf(template string, args ...interface{}) { S().Errorf(template, args...) }
func Errorw(msg string, keysAndValues ...interface{}) { S().Errorw(msg, keysAndValues...) }
func Panic(args ...interface{}) { S().Panic(args...) }
func Panicln(args ...interface{}) {
message := fmt.Sprintln(args...)
message = strings.TrimSuffix(message, "\n")
S().Panic(message)
}
func Panicf(template string, args ...interface{}) { S().Panicf(template, args...) }
func Panicw(msg string, keysAndValues ...interface{}) { S().Panicw(msg, keysAndValues...) }
func Fatal(args ...interface{}) { S().Fatal(args...) }
func Fatalln(args ...interface{}) {
message := fmt.Sprintln(args...)
message = strings.TrimSuffix(message, "\n")
S().Fatal(message)
}
func Fatalf(template string, args ...interface{}) { S().Fatalf(template, args...) }
func Fatalw(msg string, keysAndValues ...interface{}) { S().Fatalw(msg, keysAndValues...) }