This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
if_logger.go
93 lines (79 loc) · 1.8 KB
/
if_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
package log
// IfLogger implements Context
type IfLogger byte
func (il IfLogger) ok() bool { return il&0x0F != 0 }
func (il IfLogger) If(ok bool) IfLogger {
return If(ok)
}
func (il IfLogger) Else() IfLogger {
p := il & 0xF0
if p != 0 {
return p
}
return 0xFF
}
func (il IfLogger) ElseIf(ok bool) IfLogger {
p := il & 0xF0
if p != 0 {
return p
}
if ok {
return 0xFF
} else {
return 0x00
}
}
// With implements Context.With method
func (il IfLogger) With(values ...interface{}) ContextLogger {
if len(values) == 1 {
return &contextLogger{isTrue: il.ok(), data: values[0]}
}
return &contextLogger{isTrue: il.ok(), data: values}
}
// WithJSON implements Context.WithJSON method
func (il IfLogger) WithJSON(values ...interface{}) ContextLogger {
return il.With(values...).SetFormatter(jsonFormatter)
}
// SetFormatter implements Context.SetFormatter method
func (il IfLogger) SetFormatter(f Formatter) ContextLogger {
return &contextLogger{
isTrue: il.ok(),
formatter: f,
}
}
func (il IfLogger) Trace(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Trace(1, format, args...)
}
return il
}
func (il IfLogger) Debug(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Debug(1, format, args...)
}
return il
}
func (il IfLogger) Info(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Info(1, format, args...)
}
return il
}
func (il IfLogger) Warn(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Warn(1, format, args...)
}
return il
}
func (il IfLogger) Error(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Error(1, format, args...)
}
return il
}
func (il IfLogger) Fatal(format string, args ...interface{}) IfLogger {
if il.ok() {
glogger.Fatal(1, format, args...)
}
return il
}