forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
297 lines (255 loc) · 9.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
// Package log provides a log for the framework and applications.
package log
import (
"context"
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/internal/env"
)
var traceEnabled = traceEnableFromEnv()
// traceEnableFromEnv checks whether trace is enabled by reading from environment.
// Close trace if empty or zero, open trace if not zero, default as closed.
func traceEnableFromEnv() bool {
switch os.Getenv(env.LogTrace) {
case "":
fallthrough
case "0":
return false
default:
return true
}
}
// EnableTrace enables trace.
func EnableTrace() {
traceEnabled = true
}
// SetLevel sets log level for different output which may be "0", "1" or "2".
func SetLevel(output string, level Level) {
GetDefaultLogger().SetLevel(output, level)
}
// GetLevel gets log level for different output.
func GetLevel(output string) Level {
return GetDefaultLogger().GetLevel(output)
}
// With adds user defined fields to Logger. Field support multiple values.
func With(fields ...Field) Logger {
if ol, ok := GetDefaultLogger().(OptionLogger); ok {
return ol.WithOptions(WithAdditionalCallerSkip(-1)).With(fields...)
}
return GetDefaultLogger().With(fields...)
}
// WithContext add user defined fields to the Logger of context. Fields support multiple values.
func WithContext(ctx context.Context, fields ...Field) Logger {
logger, ok := codec.Message(ctx).Logger().(Logger)
if !ok {
return With(fields...)
}
if ol, ok := logger.(OptionLogger); ok {
return ol.WithOptions(WithAdditionalCallerSkip(-1)).With(fields...)
}
return logger.With(fields...)
}
// RedirectStdLog redirects std log to trpc logger as log level INFO.
// After redirection, log flag is zero, the prefix is empty.
// The returned function may be used to recover log flag and prefix, and redirect output to
// os.Stderr.
func RedirectStdLog(logger Logger) (func(), error) {
return RedirectStdLogAt(logger, zap.InfoLevel)
}
// RedirectStdLogAt redirects std log to trpc logger with a specific level.
// After redirection, log flag is zero, the prefix is empty.
// The returned function may be used to recover log flag and prefix, and redirect output to
// os.Stderr.
func RedirectStdLogAt(logger Logger, level zapcore.Level) (func(), error) {
if l, ok := logger.(*zapLog); ok {
return zap.RedirectStdLogAt(l.logger, level)
}
return nil, fmt.Errorf("log: only supports redirecting std logs to trpc zap logger")
}
// Trace logs to TRACE log. Arguments are handled in the manner of fmt.Print.
func Trace(args ...interface{}) {
if traceEnabled {
GetDefaultLogger().Trace(args...)
}
}
// Tracef logs to TRACE log. Arguments are handled in the manner of fmt.Printf.
func Tracef(format string, args ...interface{}) {
if traceEnabled {
GetDefaultLogger().Tracef(format, args...)
}
}
// TraceContext logs to TRACE log. Arguments are handled in the manner of fmt.Print.
func TraceContext(ctx context.Context, args ...interface{}) {
if !traceEnabled {
return
}
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Trace(args...)
return
}
GetDefaultLogger().Trace(args...)
}
// TraceContextf logs to TRACE log. Arguments are handled in the manner of fmt.Printf.
func TraceContextf(ctx context.Context, format string, args ...interface{}) {
if !traceEnabled {
return
}
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Tracef(format, args...)
return
}
GetDefaultLogger().Tracef(format, args...)
}
// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
func Debug(args ...interface{}) {
GetDefaultLogger().Debug(args...)
}
// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, args ...interface{}) {
GetDefaultLogger().Debugf(format, args...)
}
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func Info(args ...interface{}) {
GetDefaultLogger().Info(args...)
}
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func Infof(format string, args ...interface{}) {
GetDefaultLogger().Infof(format, args...)
}
// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func Warn(args ...interface{}) {
GetDefaultLogger().Warn(args...)
}
// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func Warnf(format string, args ...interface{}) {
GetDefaultLogger().Warnf(format, args...)
}
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func Error(args ...interface{}) {
GetDefaultLogger().Error(args...)
}
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, args ...interface{}) {
GetDefaultLogger().Errorf(format, args...)
}
// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
// All Fatal logs will exit by calling os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func Fatal(args ...interface{}) {
GetDefaultLogger().Fatal(args...)
}
// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, args ...interface{}) {
GetDefaultLogger().Fatalf(format, args...)
}
// WithContextFields sets some user defined data to logs, such as uid, imei, etc.
// Fields must be paired.
// If ctx has already set a Msg, this function returns that ctx, otherwise, it returns a new one.
func WithContextFields(ctx context.Context, fields ...string) context.Context {
tagCapacity := len(fields) / 2
tags := make([]Field, 0, tagCapacity)
for i := 0; i < tagCapacity; i++ {
tags = append(tags, Field{
Key: fields[2*i],
Value: fields[2*i+1],
})
}
ctx, msg := codec.EnsureMessage(ctx)
logger, ok := msg.Logger().(Logger)
if ok && logger != nil {
logger = logger.With(tags...)
} else {
logger = GetDefaultLogger().With(tags...)
}
msg.WithLogger(logger)
return ctx
}
// DebugContext logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
func DebugContext(ctx context.Context, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Debug(args...)
return
}
GetDefaultLogger().Debug(args...)
}
// DebugContextf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
func DebugContextf(ctx context.Context, format string, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Debugf(format, args...)
return
}
GetDefaultLogger().Debugf(format, args...)
}
// InfoContext logs to INFO log. Arguments are handled in the manner of fmt.Print.
func InfoContext(ctx context.Context, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Info(args...)
return
}
GetDefaultLogger().Info(args...)
}
// InfoContextf logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func InfoContextf(ctx context.Context, format string, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Infof(format, args...)
return
}
GetDefaultLogger().Infof(format, args...)
}
// WarnContext logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func WarnContext(ctx context.Context, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Warn(args...)
return
}
GetDefaultLogger().Warn(args...)
}
// WarnContextf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func WarnContextf(ctx context.Context, format string, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Warnf(format, args...)
return
}
GetDefaultLogger().Warnf(format, args...)
}
// ErrorContext logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func ErrorContext(ctx context.Context, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Error(args...)
return
}
GetDefaultLogger().Error(args...)
}
// ErrorContextf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func ErrorContextf(ctx context.Context, format string, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Errorf(format, args...)
return
}
GetDefaultLogger().Errorf(format, args...)
}
// FatalContext logs to ERROR log. Arguments are handled in the manner of fmt.Print.
// All Fatal logs will exit by calling os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func FatalContext(ctx context.Context, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Fatal(args...)
return
}
GetDefaultLogger().Fatal(args...)
}
// FatalContextf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func FatalContextf(ctx context.Context, format string, args ...interface{}) {
if l, ok := codec.Message(ctx).Logger().(Logger); ok {
l.Fatalf(format, args...)
return
}
GetDefaultLogger().Fatalf(format, args...)
}