-
Notifications
You must be signed in to change notification settings - Fork 824
/
default.go
272 lines (219 loc) · 6.94 KB
/
default.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
/*
* Copyright 2021 CloudWeGo Authors
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package klog
import (
"context"
"fmt"
"io"
"log"
"os"
)
var logger FullLogger = &defaultLogger{
level: LevelInfo,
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
}
// SetOutput sets the output of default logger. By default, it is stderr.
func SetOutput(w io.Writer) {
logger.SetOutput(w)
}
// SetLevel sets the level of logs below which logs will not be output.
// The default log level is LevelTrace.
// Note that this method is not concurrent-safe.
func SetLevel(lv Level) {
logger.SetLevel(lv)
}
// DefaultLogger return the default logger for kitex.
func DefaultLogger() FullLogger {
return logger
}
// SetLogger sets the default logger.
// Note that this method is not concurrent-safe and must not be called
// after the use of DefaultLogger and global functions in this package.
func SetLogger(v FullLogger) {
logger = v
}
// Fatal calls the default logger's Fatal method and then os.Exit(1).
func Fatal(v ...interface{}) {
logger.Fatal(v...)
}
// Error calls the default logger's Error method.
func Error(v ...interface{}) {
logger.Error(v...)
}
// Warn calls the default logger's Warn method.
func Warn(v ...interface{}) {
logger.Warn(v...)
}
// Notice calls the default logger's Notice method.
func Notice(v ...interface{}) {
logger.Notice(v...)
}
// Info calls the default logger's Info method.
func Info(v ...interface{}) {
logger.Info(v...)
}
// Debug calls the default logger's Debug method.
func Debug(v ...interface{}) {
logger.Debug(v...)
}
// Trace calls the default logger's Trace method.
func Trace(v ...interface{}) {
logger.Trace(v...)
}
// Fatalf calls the default logger's Fatalf method and then os.Exit(1).
func Fatalf(format string, v ...interface{}) {
logger.Fatalf(format, v...)
}
// Errorf calls the default logger's Errorf method.
func Errorf(format string, v ...interface{}) {
logger.Errorf(format, v...)
}
// Warnf calls the default logger's Warnf method.
func Warnf(format string, v ...interface{}) {
logger.Warnf(format, v...)
}
// Noticef calls the default logger's Noticef method.
func Noticef(format string, v ...interface{}) {
logger.Noticef(format, v...)
}
// Infof calls the default logger's Infof method.
func Infof(format string, v ...interface{}) {
logger.Infof(format, v...)
}
// Debugf calls the default logger's Debugf method.
func Debugf(format string, v ...interface{}) {
logger.Debugf(format, v...)
}
// Tracef calls the default logger's Tracef method.
func Tracef(format string, v ...interface{}) {
logger.Tracef(format, v...)
}
// CtxFatalf calls the default logger's CtxFatalf method and then os.Exit(1).
func CtxFatalf(ctx context.Context, format string, v ...interface{}) {
logger.CtxFatalf(ctx, format, v...)
}
// CtxErrorf calls the default logger's CtxErrorf method.
func CtxErrorf(ctx context.Context, format string, v ...interface{}) {
logger.CtxErrorf(ctx, format, v...)
}
// CtxWarnf calls the default logger's CtxWarnf method.
func CtxWarnf(ctx context.Context, format string, v ...interface{}) {
logger.CtxWarnf(ctx, format, v...)
}
// CtxNoticef calls the default logger's CtxNoticef method.
func CtxNoticef(ctx context.Context, format string, v ...interface{}) {
logger.CtxNoticef(ctx, format, v...)
}
// CtxInfof calls the default logger's CtxInfof method.
func CtxInfof(ctx context.Context, format string, v ...interface{}) {
logger.CtxInfof(ctx, format, v...)
}
// CtxDebugf calls the default logger's CtxDebugf method.
func CtxDebugf(ctx context.Context, format string, v ...interface{}) {
logger.CtxDebugf(ctx, format, v...)
}
// CtxTracef calls the default logger's CtxTracef method.
func CtxTracef(ctx context.Context, format string, v ...interface{}) {
logger.CtxTracef(ctx, format, v...)
}
type defaultLogger struct {
stdlog *log.Logger
level Level
}
func (ll *defaultLogger) SetOutput(w io.Writer) {
ll.stdlog.SetOutput(w)
}
func (ll *defaultLogger) SetLevel(lv Level) {
ll.level = lv
}
func (ll *defaultLogger) logf(lv Level, format *string, v ...interface{}) {
if ll.level > lv {
return
}
msg := lv.toString()
if format != nil {
msg += fmt.Sprintf(*format, v...)
} else {
msg += fmt.Sprint(v...)
}
ll.stdlog.Output(4, msg)
if lv == LevelFatal {
os.Exit(1)
}
}
func (ll *defaultLogger) Fatal(v ...interface{}) {
ll.logf(LevelFatal, nil, v...)
}
func (ll *defaultLogger) Error(v ...interface{}) {
ll.logf(LevelError, nil, v...)
}
func (ll *defaultLogger) Warn(v ...interface{}) {
ll.logf(LevelWarn, nil, v...)
}
func (ll *defaultLogger) Notice(v ...interface{}) {
ll.logf(LevelNotice, nil, v...)
}
func (ll *defaultLogger) Info(v ...interface{}) {
ll.logf(LevelInfo, nil, v...)
}
func (ll *defaultLogger) Debug(v ...interface{}) {
ll.logf(LevelDebug, nil, v...)
}
func (ll *defaultLogger) Trace(v ...interface{}) {
ll.logf(LevelTrace, nil, v...)
}
func (ll *defaultLogger) Fatalf(format string, v ...interface{}) {
ll.logf(LevelFatal, &format, v...)
}
func (ll *defaultLogger) Errorf(format string, v ...interface{}) {
ll.logf(LevelError, &format, v...)
}
func (ll *defaultLogger) Warnf(format string, v ...interface{}) {
ll.logf(LevelWarn, &format, v...)
}
func (ll *defaultLogger) Noticef(format string, v ...interface{}) {
ll.logf(LevelNotice, &format, v...)
}
func (ll *defaultLogger) Infof(format string, v ...interface{}) {
ll.logf(LevelInfo, &format, v...)
}
func (ll *defaultLogger) Debugf(format string, v ...interface{}) {
ll.logf(LevelDebug, &format, v...)
}
func (ll *defaultLogger) Tracef(format string, v ...interface{}) {
ll.logf(LevelTrace, &format, v...)
}
func (ll *defaultLogger) CtxFatalf(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelFatal, &format, v...)
}
func (ll *defaultLogger) CtxErrorf(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelError, &format, v...)
}
func (ll *defaultLogger) CtxWarnf(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelWarn, &format, v...)
}
func (ll *defaultLogger) CtxNoticef(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelNotice, &format, v...)
}
func (ll *defaultLogger) CtxInfof(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelInfo, &format, v...)
}
func (ll *defaultLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelDebug, &format, v...)
}
func (ll *defaultLogger) CtxTracef(ctx context.Context, format string, v ...interface{}) {
ll.logf(LevelTrace, &format, v...)
}