-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
252 lines (213 loc) · 5.71 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
package log
import (
"fmt"
"io"
"os"
"sync"
"time"
)
// default prefixes
const (
debugPrefix = "debug"
infoPrefix = "info"
warningPrefix = "warning"
errorPrefix = "error"
fatalPrefix = "fatal"
)
// default time format
const (
timeFormat = time.RFC3339
)
// logging levels
const (
debugLevel = iota
infoLevel
warningLevel
errorLevel
fatalLevel
)
// logger represents logger
type logger struct {
mutex *sync.Mutex
output io.Writer
buffer []byte
loggingLevel int
timeFormat string
debugPrefix string
infoPrefix string
warningPrefix string
errorPrefix string
fatalPrefix string
exitFunction func()
hookFunction func(prefix, message string)
}
// NewLogger creates and returns a new logger instance with given output and default parameters
func NewLogger(output io.Writer) *logger {
l := &logger{
mutex: &sync.Mutex{},
output: output,
loggingLevel: debugLevel,
timeFormat: timeFormat,
debugPrefix: debugPrefix,
infoPrefix: infoPrefix,
warningPrefix: warningPrefix,
errorPrefix: errorPrefix,
fatalPrefix: fatalPrefix,
exitFunction: func() { os.Exit(1) },
hookFunction: func(prefix, message string) {},
}
return l
}
// LoggingLevel returns logging level of logger
func (l *logger) LoggingLevel() int {
return l.loggingLevel
}
// SetLoggingLevel sets logging level of logger
func (l *logger) SetLoggingLevel(loggingLevel int) {
l.loggingLevel = loggingLevel
}
// TimeFormat returns time format of logger
func (l *logger) TimeFormat() string {
return l.timeFormat
}
// SetTimeFormat sets time format of loggger
func (l *logger) SetTimeFormat(timeFormat string) {
l.timeFormat = timeFormat
}
// DebugPrefix returns debug prefix of logger
func (l *logger) DebugPrefix() string {
return l.debugPrefix
}
// SetDebugPrefix sets debug prefix of logger
func (l *logger) SetDebugPrefix(debugPrefix string) {
l.debugPrefix = debugPrefix
}
// InfoPrefix returns info prefix of logger
func (l *logger) InfoPrefix() string {
return l.infoPrefix
}
// SetInfoPrefix sets info prefix of logger
func (l *logger) SetInfoPrefix(infoPrefix string) {
l.infoPrefix = infoPrefix
}
// WarningPrefix returns warning prefix of logger
func (l *logger) WarningPrefix() string {
return l.warningPrefix
}
// SetWarningPrefix sets warning prefix of logger
func (l *logger) SetWarningPrefix(warningPrefix string) {
l.warningPrefix = warningPrefix
}
// ErrorPrefix returns error prefix of logger
func (l *logger) ErrorPrefix() string {
return l.errorPrefix
}
// SetErrorPrefix sets error prefix of logger
func (l *logger) SetErrorPrefix(errorPrefix string) {
l.errorPrefix = errorPrefix
}
// FatalPrefix returns fatal prefix of logger
func (l *logger) FatalPrefix() string {
return l.fatalPrefix
}
// SetFatalPrefix sets fatal prefix of logger
func (l *logger) SetFatalPrefix(fatalPrefix string) {
l.fatalPrefix = fatalPrefix
}
// SetHookFunction sets hook function
func (l *logger) SetHookFunction(hookFunction func(prefix, message string)) {
l.hookFunction = hookFunction
}
// Debug logs a message with debug prefix
func (l *logger) Debug(values ...interface{}) {
if l.loggingLevel > debugLevel {
return
}
l.log(l.debugPrefix, fmt.Sprint(values...))
}
// Debugf logs a formatted message with debug prefix
func (l *logger) Debugf(format string, values ...interface{}) {
if l.loggingLevel > debugLevel {
return
}
l.log(l.debugPrefix, fmt.Sprintf(format, values...))
}
// Info logs a message with info prefix
func (l *logger) Info(values ...interface{}) {
if l.loggingLevel > infoLevel {
return
}
l.log(l.infoPrefix, fmt.Sprint(values...))
}
// Infof logs a formatted message with info prefix
func (l *logger) Infof(format string, values ...interface{}) {
if l.loggingLevel > infoLevel {
return
}
l.log(l.infoPrefix, fmt.Sprintf(format, values...))
}
// Warning logs a message with warning prefix
func (l *logger) Warning(values ...interface{}) {
if l.loggingLevel > warningLevel {
return
}
l.log(l.warningPrefix, fmt.Sprint(values...))
}
// Warningf logs a formatted message with warning prefix
func (l *logger) Warningf(format string, values ...interface{}) {
if l.loggingLevel > warningLevel {
return
}
l.log(l.warningPrefix, fmt.Sprintf(format, values...))
}
// Error logs a message with error prefix
func (l *logger) Error(values ...interface{}) {
if l.loggingLevel > errorLevel {
return
}
l.log(l.errorPrefix, fmt.Sprint(values...))
}
// Errorf logs a formatted message with error prefix
func (l *logger) Errorf(format string, values ...interface{}) {
if l.loggingLevel > errorLevel {
return
}
l.log(l.errorPrefix, fmt.Sprintf(format, values...))
}
// Fatal logs a message with fatal prefix and calls os.Exit(1)
func (l *logger) Fatal(values ...interface{}) {
if l.loggingLevel > fatalLevel {
return
}
l.log(l.fatalPrefix, fmt.Sprint(values...))
l.exitFunction()
}
// Fatalf logs a formatted message with fatal prefix and calls os.Exit(1)
func (l *logger) Fatalf(format string, values ...interface{}) {
if l.loggingLevel > fatalLevel {
return
}
l.log(l.fatalPrefix, fmt.Sprintf(format, values...))
l.exitFunction()
}
func (l *logger) log(prefix, message string) {
// synchronization
l.mutex.Lock()
defer l.mutex.Unlock()
// clear buffer
l.buffer = l.buffer[:0]
// append timestamp to buffer
l.buffer = time.Now().AppendFormat(l.buffer, l.timeFormat)
l.buffer = append(l.buffer, " "...)
//append prefix to buffer
l.buffer = append(l.buffer, prefix...)
l.buffer = append(l.buffer, " "...)
// append message to buffer
l.buffer = append(l.buffer, message...)
// append new line character
l.buffer = append(l.buffer, "\n"...)
// write buffer to output
l.output.Write(l.buffer)
// call hook function
l.hookFunction(prefix, message)
}