-
Notifications
You must be signed in to change notification settings - Fork 75
/
category.go
328 lines (294 loc) · 8.32 KB
/
category.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package log4go
import (
"fmt"
"runtime"
"strings"
"time"
)
// LOGGER get the log Filter by category
func LOGGER(category string) *Filter {
f, ok := Global[category]
if !ok {
f = &Filter{CRITICAL, NewConsoleLogWriter(), "DEFAULT"}
} else {
f.Category = category
}
return f
}
// Send a formatted log message internally
func (f *Filter) intLogf(lvl Level, format string, args ...interface{}) {
skip := true
// Determine if any logging will be done
if lvl >= f.Level {
skip = false
}
if skip {
return
}
// Determine caller func
pc, _, lineno, ok := runtime.Caller(2)
src := ""
if ok {
src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
}
msg := format
if len(args) > 0 {
msg = fmt.Sprintf(format, args...)
}
// Make the log record
rec := &LogRecord{
Level: lvl,
Created: time.Now(),
Source: src,
Message: msg,
Category: f.Category,
}
// Dispatch the logs
/*for _, filt := range log {
if lvl < filt.Level {
continue
}
filt.LogWrite(rec)
}
*/
default_filter := Global["stdout"]
if default_filter != nil && lvl > default_filter.Level {
default_filter.LogWrite(rec)
}
if f.Category != "DEFAULT" && f.Category != "stdout" {
f.LogWrite(rec)
}
}
// Send a closure log message internally
func (f *Filter) intLogc(lvl Level, closure func() string) {
skip := true
// Determine if any logging will be done
if lvl >= f.Level {
skip = false
}
if skip {
return
}
// Determine caller func
pc, _, lineno, ok := runtime.Caller(2)
src := ""
if ok {
src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
}
// Make the log record
rec := &LogRecord{
Level: lvl,
Created: time.Now(),
Source: src,
Message: closure(),
Category: f.Category,
}
default_filter := Global["stdout"]
if default_filter != nil && lvl > default_filter.Level {
default_filter.LogWrite(rec)
}
if f.Category != "DEFAULT" && f.Category != "stdout" {
f.LogWrite(rec)
}
}
// Send a log message with manual level, source, and message.
func (f *Filter) Log(lvl Level, source, message string) {
skip := true
// Determine if any logging will be done
if lvl >= f.Level {
skip = false
}
if skip {
return
}
// Make the log record
rec := &LogRecord{
Level: lvl,
Created: time.Now(),
Source: source,
Message: message,
Category: f.Category,
}
default_filter := Global["stdout"]
if default_filter != nil && lvl > default_filter.Level {
default_filter.LogWrite(rec)
}
if f.Category != "DEFAULT" && f.Category != "stdout" {
f.LogWrite(rec)
}
}
// Logf logs a formatted log message at the given log level, using the caller as
// its source.
func (f *Filter) Logf(lvl Level, format string, args ...interface{}) {
f.intLogf(lvl, format, args...)
}
// Logc logs a string returned by the closure at the given log level, using the caller as
// its source. If no log message would be written, the closure is never called.
func (f *Filter) Logc(lvl Level, closure func() string) {
f.intLogc(lvl, closure)
}
// Finest logs a message at the finest log level.
// See Debug for an explanation of the arguments.
func (f *Filter) Finest(arg0 interface{}, args ...interface{}) {
const (
lvl = FINEST
)
switch first := arg0.(type) {
case string:
// Use the string as a format string
f.intLogf(lvl, first, args...)
case func() string:
// Log the closure (no other arguments used)
f.intLogc(lvl, first)
default:
// Build a format string so that it will be similar to Sprint
f.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
// Fine logs a message at the fine log level.
// See Debug for an explanation of the arguments.
func (f *Filter) Fine(arg0 interface{}, args ...interface{}) {
const (
lvl = FINE
)
switch first := arg0.(type) {
case string:
// Use the string as a format string
f.intLogf(lvl, first, args...)
case func() string:
// f the closure (no other arguments used)
f.intLogc(lvl, first)
default:
// Build a format string so that it will be similar to Sprint
f.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
// Debug is a utility method for debug f messages.
// The behavior of Debug depends on the first argument:
// - arg0 is a string
// When given a string as the first argument, this behaves like ff but with
// the DEBUG f level: the first argument is interpreted as a format for the
// latter arguments.
// - arg0 is a func()string
// When given a closure of type func()string, this fs the string returned by
// the closure iff it will be fged. The closure runs at most one time.
// - arg0 is interface{}
// When given anything else, the f message will be each of the arguments
// formatted with %v and separated by spaces (ala Sprint).
func (f *Filter) Debug(arg0 interface{}, args ...interface{}) {
const (
lvl = DEBUG
)
switch first := arg0.(type) {
case string:
// Use the string as a format string
f.intLogf(lvl, first, args...)
case func() string:
// f the closure (no other arguments used)
f.intLogc(lvl, first)
default:
// Build a format string so that it will be similar to Sprint
f.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
// Trace fs a message at the trace f level.
// See Debug for an explanation of the arguments.
func (f *Filter) Trace(arg0 interface{}, args ...interface{}) {
const (
lvl = TRACE
)
switch first := arg0.(type) {
case string:
// Use the string as a format string
f.intLogf(lvl, first, args...)
case func() string:
// f the closure (no other arguments used)
f.intLogc(lvl, first)
default:
// Build a format string so that it will be similar to Sprint
f.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
// Info fs a message at the info f level.
// See Debug for an explanation of the arguments.
func (f *Filter) Info(arg0 interface{}, args ...interface{}) {
const (
lvl = INFO
)
switch first := arg0.(type) {
case string:
// Use the string as a format string
f.intLogf(lvl, first, args...)
case func() string:
// f the closure (no other arguments used)
f.intLogc(lvl, first)
default:
// Build a format string so that it will be similar to Sprint
f.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
}
}
// Warn fs a message at the warning f level and returns the formatted error.
// At the warning level and higher, there is no performance benefit if the
// message is not actually fged, because all formats are processed and all
// closures are executed to format the error message.
// See Debug for further explanation of the arguments.
func (f *Filter) Warn(arg0 interface{}, args ...interface{}) {
const (
lvl = WARNING
)
var msg string
switch first := arg0.(type) {
case string:
// Use the string as a format string
msg = fmt.Sprintf(first, args...)
case func() string:
// f the closure (no other arguments used)
msg = first()
default:
// Build a format string so that it will be similar to Sprint
msg = fmt.Sprintf(fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
}
f.intLogf(lvl, msg)
}
// Error fs a message at the error f level and returns the formatted error,
// See Warn for an explanation of the performance and Debug for an explanation
// of the parameters.
func (f *Filter) Error(arg0 interface{}, args ...interface{}) {
const (
lvl = ERROR
)
var msg string
switch first := arg0.(type) {
case string:
// Use the string as a format string
msg = fmt.Sprintf(first, args...)
case func() string:
// f the closure (no other arguments used)
msg = first()
default:
// Build a format string so that it will be similar to Sprint
msg = fmt.Sprintf(fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
}
f.intLogf(lvl, msg)
}
// Critical fs a message at the critical f level and returns the formatted error,
// See Warn for an explanation of the performance and Debug for an explanation
// of the parameters.
func (f *Filter) Critical(arg0 interface{}, args ...interface{}) {
const (
lvl = CRITICAL
)
var msg string
switch first := arg0.(type) {
case string:
// Use the string as a format string
msg = fmt.Sprintf(first, args...)
case func() string:
// f the closure (no other arguments used)
msg = first()
default:
// Build a format string so that it will be similar to Sprint
msg = fmt.Sprintf(fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
}
f.intLogf(lvl, msg)
}