-
Notifications
You must be signed in to change notification settings - Fork 59
/
logger.go
433 lines (376 loc) · 10.5 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package go_logger
import (
"fmt"
"os"
"path"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
var Version = "v1.2"
const (
LOGGER_LEVEL_EMERGENCY = iota
LOGGER_LEVEL_ALERT
LOGGER_LEVEL_CRITICAL
LOGGER_LEVEL_ERROR
LOGGER_LEVEL_WARNING
LOGGER_LEVEL_NOTICE
LOGGER_LEVEL_INFO
LOGGER_LEVEL_DEBUG
)
type adapterLoggerFunc func() LoggerAbstract
type LoggerAbstract interface {
Name() string
Init(config Config) error
Write(loggerMsg *loggerMessage) error
Flush()
}
var adapters = make(map[string]adapterLoggerFunc)
var levelStringMapping = map[int]string{
LOGGER_LEVEL_EMERGENCY: "Emergency",
LOGGER_LEVEL_ALERT: "Alert",
LOGGER_LEVEL_CRITICAL: "Critical",
LOGGER_LEVEL_ERROR: "Error",
LOGGER_LEVEL_WARNING: "Warning",
LOGGER_LEVEL_NOTICE: "Notice",
LOGGER_LEVEL_INFO: "Info",
LOGGER_LEVEL_DEBUG: "Debug",
}
var defaultLoggerMessageFormat = "%millisecond_format% [%level_string%] %body%"
//Register logger adapter
func Register(adapterName string, newLog adapterLoggerFunc) {
if adapters[adapterName] != nil {
panic("logger: logger adapter " + adapterName + " already registered!")
}
if newLog == nil {
panic("logger: logger adapter " + adapterName + " is nil!")
}
adapters[adapterName] = newLog
}
type Logger struct {
lock sync.Mutex //sync lock
outputs []*outputLogger // outputs loggers
msgChan chan *loggerMessage // message channel
synchronous bool // is sync
wait sync.WaitGroup // process wait
signalChan chan string
}
type outputLogger struct {
Name string
Level int
LoggerAbstract
}
type loggerMessage struct {
Timestamp int64 `json:"timestamp"`
TimestampFormat string `json:"timestamp_format"`
Millisecond int64 `json:"millisecond"`
MillisecondFormat string `json:"millisecond_format"`
Level int `json:"level"`
LevelString string `json:"level_string"`
Body string `json:"body"`
File string `json:"file"`
Line int `json:"line"`
Function string `json:"function"`
}
//new logger
//return logger
func NewLogger() *Logger {
logger := &Logger{
outputs: []*outputLogger{},
msgChan: make(chan *loggerMessage, 10),
synchronous: true,
wait: sync.WaitGroup{},
signalChan: make(chan string, 1),
}
//default adapter console
logger.attach("console", LOGGER_LEVEL_DEBUG, &ConsoleConfig{})
return logger
}
//start attach a logger adapter
//param : adapterName console | file | database | ...
//return : error
func (logger *Logger) Attach(adapterName string, level int, config Config) error {
logger.lock.Lock()
defer logger.lock.Unlock()
return logger.attach(adapterName, level, config)
}
//attach a logger adapter after lock
//param : adapterName console | file | database | ...
//return : error
func (logger *Logger) attach(adapterName string, level int, config Config) error {
for _, output := range logger.outputs {
if output.Name == adapterName {
printError("logger: adapter " + adapterName + "already attached!")
}
}
logFun, ok := adapters[adapterName]
if !ok {
printError("logger: adapter " + adapterName + "is nil!")
}
adapterLog := logFun()
err := adapterLog.Init(config)
if err != nil {
printError("logger: adapter " + adapterName + " init failed, error: " + err.Error())
}
output := &outputLogger{
Name: adapterName,
Level: level,
LoggerAbstract: adapterLog,
}
logger.outputs = append(logger.outputs, output)
return nil
}
//start attach a logger adapter
//param : adapterName console | file | database | ...
//return : error
func (logger *Logger) Detach(adapterName string) error {
logger.lock.Lock()
defer logger.lock.Unlock()
return logger.detach(adapterName)
}
//detach a logger adapter after lock
//param : adapterName console | file | database | ...
//return : error
func (logger *Logger) detach(adapterName string) error {
outputs := []*outputLogger{}
for _, output := range logger.outputs {
if output.Name == adapterName {
continue
}
outputs = append(outputs, output)
}
logger.outputs = outputs
return nil
}
//set logger level
//params : level int
//func (logger *Logger) SetLevel(level int) {
// logger.level = level
//}
//set logger synchronous false
//params : sync bool
func (logger *Logger) SetAsync(data ...int) {
logger.lock.Lock()
defer logger.lock.Unlock()
logger.synchronous = false
msgChanLen := 100
if len(data) > 0 {
msgChanLen = data[0]
}
logger.msgChan = make(chan *loggerMessage, msgChanLen)
logger.signalChan = make(chan string, 1)
if !logger.synchronous {
go func() {
defer func() {
e := recover()
if e != nil {
fmt.Printf("%v", e)
}
}()
logger.startAsyncWrite()
}()
}
}
//write log message
//params : level int, msg string
//return : error
func (logger *Logger) Writer(level int, msg string) error {
funcName := "null"
pc, file, line, ok := runtime.Caller(2)
if !ok {
file = "null"
line = 0
} else {
funcName = runtime.FuncForPC(pc).Name()
}
_, filename := path.Split(file)
if levelStringMapping[level] == "" {
printError("logger: level " + strconv.Itoa(level) + " is illegal!")
}
loggerMsg := &loggerMessage{
Timestamp: time.Now().Unix(),
TimestampFormat: time.Now().Format("2006-01-02 15:04:05"),
Millisecond: time.Now().UnixNano() / 1e6,
MillisecondFormat: time.Now().Format("2006-01-02 15:04:05.999"),
Level: level,
LevelString: levelStringMapping[level],
Body: msg,
File: filename,
Line: line,
Function: funcName,
}
if !logger.synchronous {
logger.wait.Add(1)
logger.msgChan <- loggerMsg
} else {
logger.writeToOutputs(loggerMsg)
}
return nil
}
//sync write message to loggerOutputs
//params : loggerMessage
func (logger *Logger) writeToOutputs(loggerMsg *loggerMessage) {
for _, loggerOutput := range logger.outputs {
// write level
if loggerOutput.Level >= loggerMsg.Level {
err := loggerOutput.Write(loggerMsg)
if err != nil {
fmt.Fprintf(os.Stderr, "logger: unable write loggerMessage to adapter:%v, error: %v\n", loggerOutput.Name, err)
}
}
}
}
//start async write by read logger.msgChan
func (logger *Logger) startAsyncWrite() {
for {
select {
case loggerMsg := <-logger.msgChan:
logger.writeToOutputs(loggerMsg)
logger.wait.Done()
case signal := <-logger.signalChan:
if signal == "flush" {
logger.flush()
}
}
}
}
//flush msgChan data
func (logger *Logger) flush() {
if !logger.synchronous {
for {
if len(logger.msgChan) > 0 {
loggerMsg := <-logger.msgChan
logger.writeToOutputs(loggerMsg)
logger.wait.Done()
continue
}
break
}
for _, loggerOutput := range logger.outputs {
loggerOutput.Flush()
}
}
}
//if SetAsync() or logger.synchronous is false, must call Flush() to flush msgChan data
func (logger *Logger) Flush() {
if !logger.synchronous {
logger.signalChan <- "flush"
logger.wait.Wait()
return
}
logger.flush()
}
func (logger *Logger) LoggerLevel(levelStr string) int {
levelStr = strings.ToUpper(levelStr)
switch levelStr {
case "EMERGENCY":
return LOGGER_LEVEL_EMERGENCY
case "ALERT":
return LOGGER_LEVEL_ALERT
case "CRITICAL":
return LOGGER_LEVEL_CRITICAL
case "ERROR":
return LOGGER_LEVEL_ERROR
case "WARNING":
return LOGGER_LEVEL_WARNING
case "NOTICE":
return LOGGER_LEVEL_NOTICE
case "INFO":
return LOGGER_LEVEL_INFO
case "DEBUG":
return LOGGER_LEVEL_DEBUG
default:
return LOGGER_LEVEL_DEBUG
}
}
func loggerMessageFormat(format string, loggerMsg *loggerMessage) string {
message := strings.Replace(format, "%timestamp%", strconv.FormatInt(loggerMsg.Timestamp, 10), 1)
message = strings.Replace(message, "%timestamp_format%", loggerMsg.TimestampFormat, 1)
message = strings.Replace(message, "%millisecond%", strconv.FormatInt(loggerMsg.Millisecond, 10), 1)
message = strings.Replace(message, "%millisecond_format%", loggerMsg.MillisecondFormat, 1)
message = strings.Replace(message, "%level%", strconv.Itoa(loggerMsg.Level), 1)
message = strings.Replace(message, "%level_string%", loggerMsg.LevelString, 1)
message = strings.Replace(message, "%file%", loggerMsg.File, 1)
message = strings.Replace(message, "%line%", strconv.Itoa(loggerMsg.Line), 1)
message = strings.Replace(message, "%function%", loggerMsg.Function, 1)
message = strings.Replace(message, "%body%", loggerMsg.Body, 1)
return message
}
//log emergency level
func (logger *Logger) Emergency(msg string) {
logger.Writer(LOGGER_LEVEL_EMERGENCY, msg)
}
//log emergency format
func (logger *Logger) Emergencyf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_EMERGENCY, msg)
}
//log alert level
func (logger *Logger) Alert(msg string) {
logger.Writer(LOGGER_LEVEL_ALERT, msg)
}
//log alert format
func (logger *Logger) Alertf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_ALERT, msg)
}
//log critical level
func (logger *Logger) Critical(msg string) {
logger.Writer(LOGGER_LEVEL_CRITICAL, msg)
}
//log critical format
func (logger *Logger) Criticalf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_CRITICAL, msg)
}
//log error level
func (logger *Logger) Error(msg string) {
logger.Writer(LOGGER_LEVEL_ERROR, msg)
}
//log error format
func (logger *Logger) Errorf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_ERROR, msg)
}
//log warning level
func (logger *Logger) Warning(msg string) {
logger.Writer(LOGGER_LEVEL_WARNING, msg)
}
//log warning format
func (logger *Logger) Warningf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_WARNING, msg)
}
//log notice level
func (logger *Logger) Notice(msg string) {
logger.Writer(LOGGER_LEVEL_NOTICE, msg)
}
//log notice format
func (logger *Logger) Noticef(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_NOTICE, msg)
}
//log info level
func (logger *Logger) Info(msg string) {
logger.Writer(LOGGER_LEVEL_INFO, msg)
}
//log info format
func (logger *Logger) Infof(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_INFO, msg)
}
//log debug level
func (logger *Logger) Debug(msg string) {
logger.Writer(LOGGER_LEVEL_DEBUG, msg)
}
//log debug format
func (logger *Logger) Debugf(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
logger.Writer(LOGGER_LEVEL_DEBUG, msg)
}
func printError(message string) {
fmt.Println(message)
os.Exit(0)
}