-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
132 lines (111 loc) · 3.78 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
package logger
import (
"errors"
"log"
)
// ErrorHandler will print error and entry when logging error occurred
func ErrorHandler(error error, entry Entry) {
log.Println(error, entry)
}
// LogInterface define simplest logger contract
// See LoggerInterface for a more convenient one
type LogInterface interface {
Log(message string, level Level, field ...Field)
}
// LoggerInterface define a convenient logger contract
type LoggerInterface interface {
LogInterface
Debug(message string, field ...Field)
Info(message string, field ...Field)
Notice(message string, field ...Field)
Warning(message string, field ...Field)
Error(message string, field ...Field)
Critical(message string, field ...Field)
Alert(message string, field ...Field)
Emergency(message string, field ...Field)
}
// WrappableLoggerInterface define a contract to wrap or decorate the logger with given middleware
type WrappableLoggerInterface interface {
LoggerInterface
// Implementation should return the same logger after wrapping it
Wrap(middlewares ...MiddlewareInterface) LoggerInterface
// Implementation should return a new decorated logger
WrapNew(middlewares ...MiddlewareInterface) LoggerInterface
}
// Logger is basic implementation of WrappableLoggerInterface
type Logger struct {
handler HandlerInterface
ErrorHandler func(error, Entry)
}
// onError will handle when error occur during log
func (l *Logger) onError(error error, entry Entry) {
l.ErrorHandler(error, entry)
}
// Debug will log a debug message
func (l *Logger) Debug(message string, fields ...Field) {
l.log(message, DebugLevel, fields...)
}
// Info will log a info message
func (l *Logger) Info(message string, fields ...Field) {
l.log(message, InfoLevel, fields...)
}
// Notice will log a notice message
func (l *Logger) Notice(message string, fields ...Field) {
l.log(message, NoticeLevel, fields...)
}
// Warning will log a warning message
func (l *Logger) Warning(message string, fields ...Field) {
l.log(message, WarningLevel, fields...)
}
// Error will log a message
func (l *Logger) Error(message string, fields ...Field) {
l.log(message, ErrorLevel, fields...)
}
// Critical will log a critical message
func (l *Logger) Critical(message string, fields ...Field) {
l.log(message, CriticalLevel, fields...)
}
// Alert will log a alert message
func (l *Logger) Alert(message string, fields ...Field) {
l.log(message, AlertLevel, fields...)
}
// Emergency will log a emergency message
func (l *Logger) Emergency(message string, fields ...Field) {
l.log(message, EmergencyLevel, fields...)
}
// Log will log a message with a given level
func (l *Logger) Log(message string, level Level, fields ...Field) {
l.log(message, level, fields...)
}
func (l *Logger) log(message string, level Level, fields ...Field) {
entry := Entry{message, level, NewContext(fields...)}
if err := l.handler(entry); err != nil {
l.onError(err, entry)
}
}
// Wrap will return the logger after decorate his handler with given middleware
func (l *Logger) Wrap(middlewares ...MiddlewareInterface) LoggerInterface {
for _, middleware := range middlewares {
l.handler = middleware(l.handler)
}
return l
}
// WrapNew will return a new logger after wrap his handler with given middleware
func (l *Logger) WrapNew(middlewares ...MiddlewareInterface) LoggerInterface {
handler := l.handler
for _, middleware := range middlewares {
handler = middleware(handler)
}
return &Logger{handler: handler, ErrorHandler: l.ErrorHandler}
}
// NewNopLogger will create a new no operating logger that log nowhere
func NewNopLogger() *Logger {
return NewLogger(NopHandler)
}
// NewLogger will return a new logger
func NewLogger(handler HandlerInterface) *Logger {
if handler == nil {
panic(errors.New("handler must not be <nil>"))
}
return &Logger{handler: handler, ErrorHandler: ErrorHandler}
}