-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
195 lines (159 loc) · 5.6 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
package epilog
import (
"fmt"
"time"
"github.com/go-st/logger"
)
// DefaultHandler is a default StreamHandler with popular message formatter
var DefaultHandler = NewStreamHandler(logger.LevelDebug, NewTextFormatter("[:time:] (:level:) :message:"))
// Logger is a default implementation of ILogger interface
type Logger struct {
name string
handler IHandler
processors []IProcessor
}
// New Returns new Logger instance
func New(name string, handler IHandler) *Logger {
return &Logger{
name: name,
handler: handler,
processors: make([]IProcessor, 0),
}
}
// Copy creates copy of current logger
func (l *Logger) Copy() *Logger {
return &Logger{
name: l.name,
handler: l.handler.Copy(),
processors: l.processors,
}
}
// AddProcessor adds entry processor to logger
func (l *Logger) AddProcessor(processors ...IProcessor) {
l.processors = append(l.processors, processors...)
}
// Log logs new entry with specified level
func (l *Logger) Log(level logger.Level, args ...interface{}) {
l.log(level, args...)
}
// Debug alias for log with debug level
func (l *Logger) Debug(args ...interface{}) {
l.log(logger.LevelDebug, args...)
}
// Info alias for log with Info level
func (l *Logger) Info(args ...interface{}) {
l.log(logger.LevelInfo, args...)
}
// Notice alias for log with notice level
func (l *Logger) Notice(args ...interface{}) {
l.log(logger.LevelNotice, args...)
}
// Warning alias for log with warning level
func (l *Logger) Warning(args ...interface{}) {
l.log(logger.LevelWarning, args...)
}
// Error alias for log with error level
func (l *Logger) Error(args ...interface{}) {
l.log(logger.LevelError, args...)
}
// Critical alias for log with critical level
func (l *Logger) Critical(args ...interface{}) {
l.log(logger.LevelCritical, args...)
}
// Alert alias for log with alert level
func (l *Logger) Alert(args ...interface{}) {
l.log(logger.LevelAlert, args...)
}
// Emergency alias for log with emergency level
func (l *Logger) Emergency(args ...interface{}) {
l.log(logger.LevelEmergency, args...)
}
// Logf logs new entry with specified level
func (l *Logger) Logf(level logger.Level, format string, args ...interface{}) {
l.logf(level, format, args...)
}
// Debugf alias for log with debug level
func (l *Logger) Debugf(format string, args ...interface{}) {
l.logf(logger.LevelDebug, format, args...)
}
// Infof alias for log with info level
func (l *Logger) Infof(format string, args ...interface{}) {
l.logf(logger.LevelInfo, format, args...)
}
// Noticef alias for log with notice level
func (l *Logger) Noticef(format string, args ...interface{}) {
l.logf(logger.LevelNotice, format, args...)
}
// Warningf alias for log with warning level
func (l *Logger) Warningf(format string, args ...interface{}) {
l.logf(logger.LevelWarning, format, args...)
}
// Errorf alias for log with error level
func (l *Logger) Errorf(format string, args ...interface{}) {
l.logf(logger.LevelError, format, args...)
}
// Criticalf alias for log with critical level
func (l *Logger) Criticalf(format string, args ...interface{}) {
l.logf(logger.LevelCritical, format, args...)
}
// Alertf alias for log with alert level
func (l *Logger) Alertf(format string, args ...interface{}) {
l.logf(logger.LevelAlert, format, args...)
}
// Emergencyf alias for log with emergency level
func (l *Logger) Emergencyf(format string, args ...interface{}) {
l.logf(logger.LevelEmergency, format, args...)
}
// IsDebugEnabled returns true if logger is enabled for logger.LevelDebug and false otherwise
func (l *Logger) IsDebugEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelDebug)
}
// IsInfoEnabled returns true if logger is enabled for logger.LevelInfo and false otherwise
func (l *Logger) IsInfoEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelInfo)
}
// IsNoticeEnabled returns true if logger is enabled for logger.LevelNotice and false otherwise
func (l *Logger) IsNoticeEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelNotice)
}
// IsWarningEnabled returns true if logger is enabled for logger.LevelWarning and false otherwise
func (l *Logger) IsWarningEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelWarning)
}
// IsErrorEnabled returns true if logger is enabled for logger.LevelWarning and false otherwise
func (l *Logger) IsErrorEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelError)
}
// IsAlertEnabled returns true if logger is enabled for logger.LevelAlert and false otherwise
func (l *Logger) IsAlertEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelAlert)
}
// IsCriticalEnabled returns true if logger is enabled for logger.LevelCritical and false otherwise
func (l *Logger) IsCriticalEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelCritical)
}
// IsEmergencyEnabled returns true if logger is enabled for logger.LevelEmergency and false otherwise
func (l *Logger) IsEmergencyEnabled() bool {
return l.handler.IsEnabledFor(logger.LevelEmergency)
}
// log message with level check
func (l *Logger) log(level logger.Level, args ...interface{}) {
if l.handler.IsEnabledFor(level) {
l.handle(level, args...)
}
}
// logf message with level check (important to do heavy Sprintf after level check)
func (l *Logger) logf(level logger.Level, format string, args ...interface{}) {
if l.handler.IsEnabledFor(level) {
l.handle(level, fmt.Sprintf(format, args...))
}
}
// handle puts entry to handle
func (l *Logger) handle(level logger.Level, args ...interface{}) {
entry := NewEntry(level, time.Now(), fmt.Sprint(args...))
for _, processor := range l.processors {
processor.Process(entry)
}
entry.Fields["_module"] = l.name
l.handler.Handle(entry)
}