-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
289 lines (261 loc) · 7.04 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
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
// Copyright 2018 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a Apache-style
// license that can be found in the LICENSE file.
// copy from https://github.com/chai2010/logger
package libconfd
import (
"fmt"
"io"
"log"
"os"
"strings"
"sync/atomic"
)
var pkgLogger atomic.Value
func init() {
SetLogger(NewStdLogger(os.Stderr, "", "", 0))
}
// NewStdLogger create new logger based on std log.
// If level is empty string, use WARN as the default level.
// If flag is zore, use 'log.LstdFlags|log.Lshortfile' as the default flag.
// Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL
func NewStdLogger(out io.Writer, prefix, level string, flag int) Logger {
return newStdLogger(out, prefix, level, flag)
}
func GetLogger() Logger {
return pkgLogger.Load().(Logger)
}
func SetLogger(new Logger) (old Logger) {
if x := pkgLogger.Load(); x != nil {
old = x.(Logger)
}
pkgLogger.Store(new)
return
}
// Logger interface
//
// See https://github.com/chai2010/logger
type Logger interface {
Assert(condition bool, v ...interface{})
Assertln(condition bool, v ...interface{})
Assertf(condition bool, format string, v ...interface{})
Debug(v ...interface{})
Debugln(v ...interface{})
Debugf(format string, v ...interface{})
Info(v ...interface{})
Infoln(v ...interface{})
Infof(format string, v ...interface{})
Warning(v ...interface{})
Warningln(v ...interface{})
Warningf(format string, v ...interface{})
Error(v ...interface{})
Errorln(v ...interface{})
Errorf(format string, v ...interface{})
Panic(v ...interface{})
Panicln(v ...interface{})
Panicf(format string, v ...interface{})
Fatal(v ...interface{})
Fatalln(v ...interface{})
Fatalf(format string, v ...interface{})
// Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL
GetLevel() string
SetLevel(new string) (old string)
}
type logLevelType uint32
const (
logUnknownLevel logLevelType = iota // invalid
logDebugLevel
logInfoLevel
logWarnLevel
logErrorLevel
logPanicLevel
logFatalLevel
)
func (level logLevelType) Valid() bool {
return level >= logDebugLevel && level <= logFatalLevel
}
func newLogLevel(name string) logLevelType {
switch strings.ToUpper(name) {
case "DEBUG", "":
return logDebugLevel
case "INFO":
return logInfoLevel
case "WARN":
return logWarnLevel
case "ERROR":
return logErrorLevel
case "PANIC":
return logPanicLevel
case "FATAL":
return logFatalLevel
}
return logUnknownLevel
}
func (level logLevelType) String() string {
switch level {
case logDebugLevel:
return "DEBUG"
case logInfoLevel:
return "INFO"
case logWarnLevel:
return "WARN"
case logErrorLevel:
return "ERROR"
case logPanicLevel:
return "PANIC"
case logFatalLevel:
return "FATAL"
}
return "INVALID"
}
type stdLogger struct {
level logLevelType
*log.Logger
}
func newStdLogger(out io.Writer, prefix, level string, flag int) *stdLogger {
if flag == 0 {
flag = log.LstdFlags | log.Lshortfile
}
if level == "" {
level = "INFO"
}
p := &stdLogger{Logger: log.New(out, prefix, flag)}
p.SetLevel(level)
return p
}
func (p *stdLogger) getLevel() logLevelType {
return logLevelType(atomic.LoadUint32((*uint32)(&p.level)))
}
func (p *stdLogger) setLevel(level logLevelType) logLevelType {
return logLevelType(atomic.SwapUint32((*uint32)(&p.level), uint32(level)))
}
func (p *stdLogger) getLevelName() string {
return p.getLevel().String()
}
func (p *stdLogger) setLevelByName(levelName string) string {
level := newLogLevel(levelName)
if !level.Valid() {
panic("invalid level: " + levelName)
}
return p.setLevel(level).String()
}
func (p *stdLogger) GetLevel() string {
return p.getLevel().String()
}
func (p *stdLogger) SetLevel(new string) (old string) {
return p.setLevelByName(new)
}
func (p *stdLogger) Assert(condition bool, v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l && !condition {
p.Output(2, "[ASSERT] "+fmt.Sprint(v...))
os.Exit(1)
}
}
func (p *stdLogger) Assertln(condition bool, v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l && !condition {
p.Output(2, "[ASSERT] "+fmt.Sprintln(v...))
os.Exit(1)
}
}
func (p *stdLogger) Assertf(condition bool, format string, v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l && !condition {
p.Output(2, "[ASSERT] "+fmt.Sprintf(format, v...))
os.Exit(1)
}
}
func (p *stdLogger) Debug(v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprint(v...))
}
}
func (p *stdLogger) Debugln(v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintln(v...))
}
}
func (p *stdLogger) Debugf(format string, v ...interface{}) {
if l := logDebugLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintf(format, v...))
}
}
func (p *stdLogger) Info(v ...interface{}) {
if l := logInfoLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprint(v...))
}
}
func (p *stdLogger) Infoln(v ...interface{}) {
if l := logInfoLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintln(v...))
}
}
func (p *stdLogger) Infof(format string, v ...interface{}) {
if l := logInfoLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintf(format, v...))
}
}
func (p *stdLogger) Warning(v ...interface{}) {
if l := logWarnLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprint(v...))
}
}
func (p *stdLogger) Warningln(v ...interface{}) {
if l := logWarnLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintln(v...))
}
}
func (p *stdLogger) Warningf(format string, v ...interface{}) {
if l := logWarnLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintf(format, v...))
}
}
func (p *stdLogger) Error(v ...interface{}) {
if l := logErrorLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprint(v...))
}
}
func (p *stdLogger) Errorln(v ...interface{}) {
if l := logErrorLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintln(v...))
}
}
func (p *stdLogger) Errorf(format string, v ...interface{}) {
if l := logErrorLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+fmt.Sprintf(format, v...))
}
}
func (p *stdLogger) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
if l := logPanicLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+s)
}
panic(s)
}
func (p *stdLogger) Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
if l := logPanicLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+s)
}
panic(s)
}
func (p *stdLogger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
if l := logPanicLevel; p.getLevel() <= l {
p.Output(2, "["+l.String()+"] "+s)
}
panic(s)
}
func (p *stdLogger) Fatal(v ...interface{}) {
const l = logFatalLevel
p.Output(2, "["+l.String()+"] "+fmt.Sprint(v...))
os.Exit(1)
}
func (p *stdLogger) Fatalln(v ...interface{}) {
const l = logFatalLevel
p.Output(2, "["+l.String()+"] "+fmt.Sprintln(v...))
os.Exit(1)
}
func (p *stdLogger) Fatalf(format string, v ...interface{}) {
const l = logFatalLevel
p.Output(2, "["+l.String()+"] "+fmt.Sprintf(format, v...))
os.Exit(1)
}