-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_logger.go
198 lines (165 loc) · 4.24 KB
/
log_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
package log
import (
"context"
"os"
)
// Logger 表示一个日志器
type Logger interface {
Tracef(f string, a ...any)
Trace(a ...any)
Debugf(f string, a ...any)
Debug(a ...any)
Infof(f string, a ...any)
Info(a ...any)
Warnf(f string, a ...any)
Warn(a ...any)
Errorf(f string, a ...any)
Error(a ...any)
Fatalf(f string, a ...any)
Fatal(a ...any)
}
type nonCtxLogger interface {
logf(f string, a ...any)
log(a ...any)
}
type ctxLogger interface {
logCtxf(ctx context.Context, f string, a ...any)
logCtx(ctx context.Context, a ...any)
}
// NewLogger 返回一个日志器
func NewLogger(ctx ...context.Context) Logger {
if len(ctx) == 0 || ctx[0] == nil {
return loggerImplWithoutCtx{}
}
return &loggerImplWithCtx{ctx: ctx[0]}
}
// -------- logger without context --------
type loggerImplWithoutCtx struct{}
// Tracef 调试日志
func (loggerImplWithoutCtx) Tracef(f string, a ...any) {
l := getNonCtxLoggers(TraceLevel)
doNonCtxLogf(l, f, a...)
}
// Trace 调试日志
func (loggerImplWithoutCtx) Trace(a ...any) {
l := getNonCtxLoggers(TraceLevel)
doNonCtxLog(l, a...)
}
// Debugf 调试日志
func (loggerImplWithoutCtx) Debugf(f string, a ...any) {
l := getNonCtxLoggers(DebugLevel)
doNonCtxLogf(l, f, a...)
}
// Debug 调试日志
func (loggerImplWithoutCtx) Debug(a ...any) {
l := getNonCtxLoggers(DebugLevel)
doNonCtxLog(l, a...)
}
// Infof 信息日志
func (loggerImplWithoutCtx) Infof(f string, a ...any) {
l := getNonCtxLoggers(InfoLevel)
doNonCtxLogf(l, f, a...)
}
// Info 信息日志
func (loggerImplWithoutCtx) Info(a ...any) {
l := getNonCtxLoggers(InfoLevel)
doNonCtxLog(l, a...)
}
// Warnf 警告日志
func (loggerImplWithoutCtx) Warnf(f string, a ...any) {
l := getNonCtxLoggers(WarnLevel)
doNonCtxLogf(l, f, a...)
}
// Warn 警告日志
func (loggerImplWithoutCtx) Warn(a ...any) {
l := getNonCtxLoggers(WarnLevel)
doNonCtxLog(l, a...)
}
// Errorf 错误日志
func (loggerImplWithoutCtx) Errorf(f string, a ...any) {
l := getNonCtxLoggers(ErrorLevel)
doNonCtxLogf(l, f, a...)
}
// Error 错误日志
func (loggerImplWithoutCtx) Error(a ...any) {
l := getNonCtxLoggers(ErrorLevel)
doNonCtxLog(l, a...)
}
// Fatalf 崩溃日志
func (loggerImplWithoutCtx) Fatalf(f string, a ...any) {
l := getNonCtxLoggers(FatalLevel)
doNonCtxLogf(l, f, a...)
os.Exit(-1)
}
// Fatal 崩溃日志
func (loggerImplWithoutCtx) Fatal(a ...any) {
l := getNonCtxLoggers(FatalLevel)
doNonCtxLog(l, a...)
os.Exit(-1)
}
// -------- logger without context --------
type loggerImplWithCtx struct {
ctx context.Context
}
// Tracef 底层跟踪日志
func (c *loggerImplWithCtx) Tracef(f string, a ...any) {
l := getCtxLoggers(c.ctx, TraceLevel)
doCtxLogf(c.ctx, l, f, a...)
}
// Trace 底层跟踪日志
func (c *loggerImplWithCtx) Trace(a ...any) {
l := getCtxLoggers(c.ctx, TraceLevel)
doCtxLog(c.ctx, l, a...)
}
// Debugf 调试日志
func (c *loggerImplWithCtx) Debugf(f string, a ...any) {
l := getCtxLoggers(c.ctx, DebugLevel)
doCtxLogf(c.ctx, l, f, a...)
}
// Debug 调试日志
func (c *loggerImplWithCtx) Debug(a ...any) {
l := getCtxLoggers(c.ctx, DebugLevel)
doCtxLog(c.ctx, l, a...)
}
// Infof 信息日志
func (c *loggerImplWithCtx) Infof(f string, a ...any) {
l := getCtxLoggers(c.ctx, InfoLevel)
doCtxLogf(c.ctx, l, f, a...)
}
// Info 信息日志
func (c *loggerImplWithCtx) Info(a ...any) {
l := getCtxLoggers(c.ctx, InfoLevel)
doCtxLog(c.ctx, l, a...)
}
// Warnf 警告日志
func (c *loggerImplWithCtx) Warnf(f string, a ...any) {
l := getCtxLoggers(c.ctx, WarnLevel)
doCtxLogf(c.ctx, l, f, a...)
}
// WarnContext 警告日志
func (c *loggerImplWithCtx) Warn(a ...any) {
l := getCtxLoggers(c.ctx, WarnLevel)
doCtxLog(c.ctx, l, a...)
}
// Errorf 错误日志
func (c *loggerImplWithCtx) Errorf(f string, a ...any) {
l := getCtxLoggers(c.ctx, ErrorLevel)
doCtxLogf(c.ctx, l, f, a...)
}
// Error 错误日志
func (c *loggerImplWithCtx) Error(a ...any) {
l := getCtxLoggers(c.ctx, ErrorLevel)
doCtxLog(c.ctx, l, a...)
}
// Fatalf 崩溃日志
func (c *loggerImplWithCtx) Fatalf(f string, a ...any) {
l := getCtxLoggers(c.ctx, FatalLevel)
doCtxLogf(c.ctx, l, f, a...)
os.Exit(-1)
}
// Fatal 崩溃日志
func (c *loggerImplWithCtx) Fatal(a ...any) {
l := getCtxLoggers(c.ctx, FatalLevel)
doCtxLog(c.ctx, l, a...)
os.Exit(-1)
}