-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_level_logger.go
218 lines (165 loc) · 4.63 KB
/
std_level_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
package adaptlog
import (
"fmt"
"io"
"log"
"strings"
)
// LogLevel enum
type LogLevel uint8
// Log Level values
const (
DebugLevel LogLevel = iota
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
func (l LogLevel) String() string {
switch l {
case DebugLevel:
return "Debug"
case InfoLevel:
return "Info"
case WarnLevel:
return "Warn"
case ErrorLevel:
return "Error"
case FatalLevel:
return "Fatal"
default:
return fmt.Sprintf("Not mapped value %d", l)
}
}
var logFunc func(LogLevel, string, ...interface{})
var loglnFunc func(LogLevel, string, ...interface{})
var logfFunc func(LogLevel, string, string, ...interface{})
// StdLevelLogger is a level logger based on the standard log package
type StdLevelLogger struct {
logFunc func(LogLevel, string, ...interface{})
loglnFunc func(LogLevel, string, ...interface{})
logfFunc func(LogLevel, string, string, ...interface{})
context string
}
// NewStdLevelLogger creates a new standard level logger
func NewStdLevelLogger(context string) *StdLevelLogger {
return &StdLevelLogger{logFunc, loglnFunc, logfFunc, context}
}
// ConfigureStdLevelLogger configures the standard level logger with the default log level.
// By providing a different io.Writer and prefix you can control the logging output (testing etc)
func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer, context string) {
logFunc = func(level LogLevel, context string, args ...interface{}) {
if defaultLoglevel > level {
return
}
if w != nil {
log.SetOutput(w)
}
if context == "" {
args = prepend(args, strings.Join([]string{level.String(), " "}, ""))
} else {
args = prepend(args, level.String(), " ", context, " ")
}
log.Print(args...)
}
loglnFunc = func(level LogLevel, context string, args ...interface{}) {
if defaultLoglevel > level {
return
}
if w != nil {
log.SetOutput(w)
}
if context == "" {
args = prepend(args, level.String())
} else {
args = prepend(args, level.String(), context)
}
log.Println(args...)
}
logfFunc = func(level LogLevel, context string, msg string, args ...interface{}) {
if defaultLoglevel > level {
return
}
if w != nil {
log.SetOutput(w)
}
if context == "" {
log.Printf(strings.Join([]string{level.String(), msg}, " "), args...)
} else {
log.Printf(strings.Join([]string{level.String(), context, msg}, " "), args...)
}
}
Level = NewStdLevelLogger(context)
}
func prepend(args []interface{}, values ...string) []interface{} {
valuesLength := len(values)
argsLength := len(args)
argsExt := make([]interface{}, argsLength+valuesLength)
for index := 0; index < valuesLength; index++ {
argsExt[index] = values[index]
}
for index := valuesLength; index < argsLength+valuesLength; index++ {
argsExt[index] = args[index-valuesLength]
}
return argsExt
}
// Fatal logging
func (l *StdLevelLogger) Fatal(args ...interface{}) {
l.logFunc(FatalLevel, l.context, args...)
}
// Fatalf logging
func (l *StdLevelLogger) Fatalf(msg string, args ...interface{}) {
l.logfFunc(FatalLevel, l.context, msg, args...)
}
// Fatalln logging
func (l *StdLevelLogger) Fatalln(args ...interface{}) {
l.loglnFunc(FatalLevel, l.context, args...)
}
// Error logging
func (l *StdLevelLogger) Error(args ...interface{}) {
l.logFunc(ErrorLevel, l.context, args...)
}
// Errorf logging
func (l *StdLevelLogger) Errorf(msg string, args ...interface{}) {
l.logfFunc(ErrorLevel, l.context, msg, args...)
}
// Errorln logging
func (l *StdLevelLogger) Errorln(args ...interface{}) {
l.loglnFunc(ErrorLevel, l.context, args...)
}
// Warn logging
func (l *StdLevelLogger) Warn(args ...interface{}) {
l.logFunc(WarnLevel, l.context, args...)
}
// Warnf logging
func (l *StdLevelLogger) Warnf(msg string, args ...interface{}) {
l.logfFunc(WarnLevel, l.context, msg, args...)
}
// Warnln logging
func (l *StdLevelLogger) Warnln(args ...interface{}) {
l.loglnFunc(WarnLevel, l.context, args...)
}
// Info logging
func (l *StdLevelLogger) Info(args ...interface{}) {
l.logFunc(InfoLevel, l.context, args...)
}
// Infof logging
func (l *StdLevelLogger) Infof(msg string, args ...interface{}) {
l.logfFunc(InfoLevel, l.context, msg, args...)
}
// Infoln logging
func (l *StdLevelLogger) Infoln(args ...interface{}) {
l.loglnFunc(InfoLevel, l.context, args...)
}
// Debug logging
func (l *StdLevelLogger) Debug(args ...interface{}) {
l.logFunc(DebugLevel, l.context, args...)
}
// Debugf logging
func (l *StdLevelLogger) Debugf(msg string, args ...interface{}) {
l.logfFunc(DebugLevel, l.context, msg, args...)
}
// Debugln logging
func (l *StdLevelLogger) Debugln(args ...interface{}) {
l.loglnFunc(DebugLevel, l.context, args...)
}