-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.go
239 lines (202 loc) · 5.68 KB
/
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
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
package log
import (
"fmt"
"io"
"os"
"sync"
)
// Exit is equals os.Exit
var Exit = os.Exit
// Logger is represents an active logging object
type Logger struct {
m sync.Mutex
Level Level
Formatter Formatter
Out io.Writer
}
// New creates a new Logger
func New() *Logger {
return &Logger{
Level: INFO,
Formatter: new(simpleFormatter),
Out: os.Stdout,
}
}
// IsDebugEnabled indicates whether output message
func (l *Logger) IsDebugEnabled() bool {
return l.Level >= DEBUG
}
// IsInfoEnabled indicates whether output message
func (l *Logger) IsInfoEnabled() bool {
return l.Level >= INFO
}
// IsPrintEnabled indicates whether output message
func (l *Logger) IsPrintEnabled() bool {
return l.Level > OFF
}
// IsWarnEnabled indicates whether output message
func (l *Logger) IsWarnEnabled() bool {
return l.Level >= WARN
}
// IsErrorEnabled indicates whether output message
func (l *Logger) IsErrorEnabled() bool {
return l.Level >= ERROR
}
// IsPanicEnabled indicates whether output message
func (l *Logger) IsPanicEnabled() bool {
return l.Level >= PANIC
}
// IsFatalEnabled indicates whether output message
func (l *Logger) IsFatalEnabled() bool {
return l.Level >= FATAL
}
// IsDisabled indicates whether output message
func (l *Logger) IsDisabled() bool {
return l.Level <= OFF
}
// Debug outputs message, Arguments are handled by fmt.Sprint
func (l *Logger) Debug(obj ...interface{}) {
if l.Level >= DEBUG {
l.log(DEBUG, fmt.Sprint(obj...))
}
}
// Info outputs message, Arguments are handled by fmt.Sprint
func (l *Logger) Info(obj ...interface{}) {
if l.Level >= INFO {
l.log(INFO, fmt.Sprint(obj...))
}
}
// Print outputs message, Arguments are handled by fmt.Sprint
func (l *Logger) Print(obj ...interface{}) {
if l.Level != OFF {
l.log(INFO, fmt.Sprint(obj...))
}
}
// Warn outputs message, Arguments are handled by fmt.Sprint
func (l *Logger) Warn(obj ...interface{}) {
if l.Level >= WARN {
l.log(WARN, fmt.Sprint(obj...))
}
}
// Error outputs message, Arguments are handled by fmt.Sprint
func (l *Logger) Error(obj ...interface{}) {
if l.Level >= ERROR {
l.log(ERROR, fmt.Sprint(obj...))
}
}
// Panic outputs message, and followed by a call to panic() Arguments are handled by fmt.Sprint
func (l *Logger) Panic(obj ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, fmt.Sprint(obj...))
}
panic(fmt.Sprint(obj...))
}
// Fatal outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprint
func (l *Logger) Fatal(obj ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, fmt.Sprint(obj...))
}
Exit(1)
}
// Debugln outputs message, Arguments are handled by fmt.Sprintln
func (l *Logger) Debugln(obj ...interface{}) {
if l.Level >= DEBUG {
l.log(DEBUG, vsprintln(obj...))
}
}
// Infoln outputs message, Arguments are handled by fmt.Sprintln
func (l *Logger) Infoln(obj ...interface{}) {
if l.Level >= INFO {
l.log(INFO, vsprintln(obj...))
}
}
// Println outputs message, Arguments are handled by fmt.Sprintln
func (l *Logger) Println(obj ...interface{}) {
if l.Level != OFF {
l.log(INFO, vsprintln(obj...))
}
}
// Warnln outputs message, Arguments are handled by fmt.Sprintln
func (l *Logger) Warnln(obj ...interface{}) {
if l.Level >= WARN {
l.log(WARN, vsprintln(obj...))
}
}
// Errorln outputs message, Arguments are handled by fmt.Sprintln
func (l *Logger) Errorln(obj ...interface{}) {
if l.Level >= ERROR {
l.log(ERROR, vsprintln(obj...))
}
}
// Panicln outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintln
func (l *Logger) Panicln(obj ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, vsprintln(obj...))
}
panic(vsprintln(obj...))
}
// Fatalln outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintln
func (l *Logger) Fatalln(obj ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, vsprintln(obj...))
}
Exit(1)
}
// Debugf outputs message, Arguments are handles by fmt.Sprintf
func (l *Logger) Debugf(msg string, args ...interface{}) {
if l.Level >= DEBUG {
l.log(DEBUG, fmt.Sprintf(msg, args...))
}
}
// Infof outputs message, Arguments are handles by fmt.Sprintf
func (l *Logger) Infof(msg string, args ...interface{}) {
if l.Level >= INFO {
l.log(INFO, fmt.Sprintf(msg, args...))
}
}
// Printf outputs message, Arguments are handles by fmt.Sprintf
func (l *Logger) Printf(msg string, args ...interface{}) {
if l.Level != OFF {
l.log(INFO, fmt.Sprintf(msg, args...))
}
}
// Warnf outputs message, Arguments are handles by fmt.Sprintf
func (l *Logger) Warnf(msg string, args ...interface{}) {
if l.Level >= WARN {
l.log(WARN, fmt.Sprintf(msg, args...))
}
}
// Errorf outputs message, Arguments are handles by fmt.Sprintf
func (l *Logger) Errorf(msg string, args ...interface{}) {
if l.Level >= ERROR {
l.log(ERROR, fmt.Sprintf(msg, args...))
}
}
// Panicf outputs message and followed by a call to panic(), Arguments are handles by fmt.Sprintf
func (l *Logger) Panicf(msg string, args ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, fmt.Sprintf(msg, args...))
}
panic(fmt.Sprintf(msg, args...))
}
// Fatalf outputs message and followed by a call to os.Exit(1), Arguments are handles by fmt.Sprintf
func (l *Logger) Fatalf(msg string, args ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, fmt.Sprintf(msg, args...))
}
Exit(1)
}
func (l *Logger) log(level Level, msg string) {
line := l.Formatter.Format(level, msg, l)
l.m.Lock()
defer l.m.Unlock()
_, err := l.Out.Write(line)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write log, %v\n", err)
}
}
// vsprintln => spaces are always added between operands
func vsprintln(obj ...interface{}) string {
msg := fmt.Sprintln(obj...)
return msg[:len(msg)-1]
}