-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
244 lines (199 loc) · 7.8 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
package log
import (
"fmt"
"github.com/golang/glog"
"golang.org/x/net/context"
"github.com/binchencoder/letsgo/grpc"
"github.com/binchencoder/letsgo/trace"
)
const (
traceFormat = "(trace: %s) "
callerFormat = "(caller: %s) "
traceAndCallerFormat = "(trace: %s, caller: %s) "
)
// Switch is a boolean type with a method Context.
// See the documentation of V for more information.
type Switch bool
// Logger is a struct contains MDC for log methods. It provides functions Info,
// Warning, Error, Fatal, plus formatting variants such as Infof.
// See the documentation of github.com/golang/glog for more information.
type Logger struct {
mdc string
}
// Verbose is a variant type of Logger. It provides less log methods.
// If the value is nil, it doesn't record log.
type Verbose Logger
// One may write either
// if log.V(2) { log.Context(ctx).Info("log this") }
// or
// glog.V(2).Context(ctx).Info("log this")
// The second form is shorter but the first is cheaper if logging is off because it does
// not evaluate its arguments.
//
// See the documentation of github.com/golang/glog.V for more information.
// TODO(chenbin) 2019/08/15
// func V(level glog.Level) Switch {
// return Switch(glog.VDepth(1, level))
// }
// Context return a Logger with MDC from ctx.
func Context(ctx context.Context) *Logger {
// caller is the service name of the caller.
ctx, caller := grpc.FromMetadataIncoming(ctx)
tid := trace.GetTraceIdOrEmpty(ctx)
logger := &Logger{}
switch {
case tid != "" && caller != "":
logger.mdc = fmt.Sprintf(traceAndCallerFormat, tid, caller)
case tid != "":
logger.mdc = fmt.Sprintf(traceFormat, tid)
case caller != "":
logger.mdc = fmt.Sprintf(callerFormat, caller)
}
return logger
}
// Flush flushes log to disk.
func Flush() {
glog.Flush()
}
// Context return a Verbose with MDC from ctx.
func (s Switch) Context(ctx context.Context) *Verbose {
if s {
return (*Verbose)(Context(ctx))
}
return nil
}
func (l *Logger) args(args ...interface{}) []interface{} {
if l.mdc == "" {
return args
}
return append([]interface{}{l.mdc}, args...)
}
// Info logs to the INFO log.
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func (l *Logger) Info(args ...interface{}) {
glog.InfoDepth(1, l.args(args)...)
}
// InfoDepth acts as Info but uses depth to determine which call frame to log.
// InfoDepth(0, "msg") is the same as Info("msg").
func (l *Logger) InfoDepth(depth int, args ...interface{}) {
glog.InfoDepth(depth+1, l.args(args)...)
}
// Infoln logs to the INFO log.
// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func (l *Logger) Infoln(args ...interface{}) {
args = append(args, "\n")
glog.InfoDepth(1, l.args(args)...)
}
// Infof logs to the INFO log.
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func (l *Logger) Infof(format string, args ...interface{}) {
glog.InfoDepth(1, l.mdc, fmt.Sprintf(format, args...))
}
// Warning logs to the WARNING and INFO logs.
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func (l *Logger) Warning(args ...interface{}) {
glog.WarningDepth(1, l.args(args)...)
}
// WarningDepth acts as Warning but uses depth to determine which call frame to log.
// WarningDepth(0, "msg") is the same as Warning("msg").
func (l *Logger) WarningDepth(depth int, args ...interface{}) {
glog.WarningDepth(depth+1, l.args(args)...)
}
// Warningln logs to the WARNING and INFO logs.
// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func (l *Logger) Warningln(args ...interface{}) {
args = append(args, "\n")
glog.WarningDepth(1, l.args(args)...)
}
// Warningf logs to the WARNING and INFO logs.
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func (l *Logger) Warningf(format string, args ...interface{}) {
glog.WarningDepth(1, l.mdc, fmt.Sprintf(format, args...))
}
// Error logs to the ERROR, WARNING, and INFO logs.
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func (l *Logger) Error(args ...interface{}) {
glog.ErrorDepth(1, l.args(args)...)
}
// ErrorDepth acts as Error but uses depth to determine which call frame to log.
// ErrorDepth(0, "msg") is the same as Error("msg").
func (l *Logger) ErrorDepth(depth int, args ...interface{}) {
glog.ErrorDepth(depth+1, l.args(args)...)
}
// Errorln logs to the ERROR, WARNING, and INFO logs.
// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func (l *Logger) Errorln(args ...interface{}) {
args = append(args, "\n")
glog.ErrorDepth(1, l.args(args)...)
}
// Errorf logs to the ERROR, WARNING, and INFO logs.
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func (l *Logger) Errorf(format string, args ...interface{}) {
glog.ErrorDepth(1, l.mdc, fmt.Sprintf(format, args...))
}
// Fatal logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func (l *Logger) Fatal(args ...interface{}) {
glog.FatalDepth(1, l.args(args)...)
}
// FatalDepth acts as Fatal but uses depth to determine which call frame to log.
// FatalDepth(0, "msg") is the same as Fatal("msg").
func (l *Logger) FatalDepth(depth int, args ...interface{}) {
glog.FatalDepth(depth+1, l.args(args)...)
}
// Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func (l *Logger) Fatalln(args ...interface{}) {
args = append(args, "\n")
glog.FatalDepth(1, l.args(args)...)
}
// Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func (l *Logger) Fatalf(format string, args ...interface{}) {
glog.FatalDepth(1, l.mdc, fmt.Sprintf(format, args...))
}
// Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func (l *Logger) Exit(args ...interface{}) {
glog.ExitDepth(1, l.args(args)...)
}
// ExitDepth acts as Exit but uses depth to determine which call frame to log.
// ExitDepth(0, "msg") is the same as Exit("msg").
func (l *Logger) ExitDepth(depth int, args ...interface{}) {
glog.ExitDepth(depth+1, l.args(args)...)
}
// Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
func (l *Logger) Exitln(args ...interface{}) {
args = append(args, "\n")
glog.ExitDepth(1, l.args(args)...)
}
// Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func (l *Logger) Exitf(format string, args ...interface{}) {
glog.ExitDepth(1, l.mdc, fmt.Sprintf(format, args...))
}
// Info is equivalent to the Logger.Info function, guarded by the value of v.
// See the documentation of V for usage.
func (v *Verbose) Info(args ...interface{}) {
if v != nil {
(*Logger)(v).InfoDepth(1, args...)
}
}
// Infoln is equivalent to the Logger.Infoln function, guarded by the value of v.
// See the documentation of V for usage.
func (v *Verbose) Infoln(args ...interface{}) {
if v != nil {
args = append(args, "\n")
(*Logger)(v).InfoDepth(1, args)
}
}
// Infof is equivalent to the Logger.Infof function, guarded by the value of v.
// See the documentation of V for usage.
func (v *Verbose) Infof(format string, args ...interface{}) {
if v != nil {
(*Logger)(v).InfoDepth(1, fmt.Sprintf(format, args...))
}
}