-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
196 lines (154 loc) · 3.77 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
package log
import (
"fmt"
"os"
)
// Logger interface provides means to extend this library
type Logger interface {
// Level gives the current threshold of the logger
Level() Level
// PrintLevel gives the level at which log.Print logs
PrintLevel() Level
// Logf is the workhorse function that logs each line; works in a similar way to fmt.Printf
Logf(level Level, format string, value ...interface{})
// Close closes the logger
Close()
}
type Log struct {
logger Logger
}
func (l Log) Debugf(format string, value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Debug, format, value...)
}
func (l Log) Infof(format string, value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Info, format, value...)
}
func (l Log) Warningf(format string, value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Warning, format, value...)
}
func (l Log) Errorf(format string, value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Error, format, value...)
}
func (l Log) Printf(format string, value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(l.logger.PrintLevel(), format, value...)
}
func (l Log) Fatalf(format string, value ...interface{}) {
if l.logger != nil {
l.logger.Logf(Error, format, value...)
}
os.Exit(1)
}
func (l Log) Debug(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Debug, "%s", fmt.Sprint(value...))
}
func (l Log) Info(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Info, "%s", fmt.Sprint(value...))
}
func (l Log) Warning(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Warning, "%s", fmt.Sprint(value...))
}
func (l Log) Error(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(Error, "%s", fmt.Sprint(value...))
}
func (l Log) Print(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(l.logger.PrintLevel(), "%s", fmt.Sprint(value...))
}
func (l Log) Println(value ...interface{}) {
if l.logger == nil {
return
}
l.logger.Logf(l.logger.PrintLevel(), "%s", fmt.Sprint(value...))
}
func (l Log) Fatal(value ...interface{}) {
if l.logger != nil {
l.logger.Logf(Error, "%s", fmt.Sprint(value...))
}
os.Exit(1)
}
func (l Log) Fatalln(value ...interface{}) {
if l.logger != nil {
l.logger.Logf(Error, "%s", fmt.Sprint(value...))
}
os.Exit(1)
}
func (l Log) Panic(value ...interface{}) {
s := fmt.Sprint(value...)
if l.logger != nil {
l.logger.Logf(Error, "%s", s)
}
panic(s)
}
func (l Log) Panicf(format string, value ...interface{}) {
s := fmt.Sprintf(format, value...)
if l.logger != nil {
l.logger.Logf(Error, "%s", s)
}
panic(s)
}
func (l Log) Panicln(value ...interface{}) {
s := fmt.Sprint(value...)
if l.logger != nil {
l.logger.Logf(Error, "%s", s)
}
panic(s)
}
// DebugEnabled checks if DEBUG level is enabled for the logger.
// It can be used to check before performing any extra processing to generate data
// that is purely for logging, thereby avoiding the extra processing when DEBUG
// level is disabled.
//
// Example:
// if logger.DebugEnabled() {
// debugData := makeDebugData()
// logger.Debugf("debug data: %v", debugData)
// }
func (l Log) DebugEnabled() bool {
if l.logger == nil {
return false
}
return Debug.IsEnabled(l.logger.Level())
}
// Close disables and closes the logger, freeing up any resources allocated to the logger.
// Once closed the logger will be disabled but it will remain safe to use (free from panics).
func (l Log) Close() {
if l.logger != nil {
l.logger.Close()
}
}
// Must ensures that a Log instance was initialised without error; panics if there was an error.
func Must(l Log, err error) Log {
if err != nil {
panic(fmt.Errorf("failed to initialise logger; %w", err))
}
return l
}