-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
227 lines (188 loc) · 5.21 KB
/
main.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
// Package log provides logging services. All logging goes through this layer so that we can
// easily change the logging implementation.
// The goal here is to be able to redirect logs with levels warn and error to stderr and have the rest
// go to stdout.
package log
import (
"fmt"
"io"
golog "log"
"os"
)
type LogLevel int
const (
TraceLevel LogLevel = iota
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
const defaultLevel = InfoLevel
func (level LogLevel) String() string {
if level < TraceLevel || level > FatalLevel {
return "unknown"
}
return [...]string{"trace", "debug", "info", "warn", "error", "fatal"}[level]
}
type Logger struct {
logLevel LogLevel
traceLogger golog.Logger
debugLogger golog.Logger
infoLogger golog.Logger
warnLogger golog.Logger
errorLogger golog.Logger
fatalLogger golog.Logger
}
const defaultOpts = golog.LstdFlags | golog.Lmsgprefix
// NewLogger creates a new logger. It takes two io.Writers, one for trace,debug,info and one for warn,error,fatal logs.
func NewLogger(infoOutput, errorOutput io.Writer) *Logger {
l := &Logger{
logLevel: defaultLevel,
traceLogger: *golog.New(infoOutput, "trace ", defaultOpts),
debugLogger: *golog.New(infoOutput, "debug ", defaultOpts),
infoLogger: *golog.New(infoOutput, "info ", defaultOpts),
warnLogger: *golog.New(errorOutput, "warn ", defaultOpts),
errorLogger: *golog.New(errorOutput, "error ", defaultOpts),
fatalLogger: *golog.New(errorOutput, "fatal ", defaultOpts),
}
return l
}
// NewWithPrefix creates a new logger. It takes two io.Writers, one for trace,debug,info and one for warn,error,fatal logs.
// It also takes a prefix which will be added to all log messages.
func NewWithPrefix(infoOutput, errorOutput io.Writer, prefix string) *Logger {
l := &Logger{
logLevel: defaultLevel,
traceLogger: *golog.New(infoOutput, prefix+" trace ", defaultOpts),
debugLogger: *golog.New(infoOutput, prefix+" debug ", defaultOpts),
infoLogger: *golog.New(infoOutput, prefix+" info ", defaultOpts),
warnLogger: *golog.New(errorOutput, prefix+" warn ", defaultOpts),
errorLogger: *golog.New(errorOutput, prefix+" error ", defaultOpts),
fatalLogger: *golog.New(errorOutput, prefix+" fatal ", defaultOpts),
}
return l
}
var defaultLogger *Logger
func init() {
defaultLogger = NewLogger(os.Stdout, os.Stderr)
}
func Trace(v ...interface{}) {
defaultLogger.Trace(v...)
}
func Debug(v ...interface{}) {
defaultLogger.Debug(v...)
}
func Info(v ...interface{}) {
defaultLogger.Info(v...)
}
func Warn(v ...interface{}) {
defaultLogger.Warn(v...)
}
func Error(v ...interface{}) {
defaultLogger.Error(v...)
}
func Fatal(v ...interface{}) {
defaultLogger.Fatal(v...)
}
func Tracef(format string, v ...interface{}) {
defaultLogger.Tracef(format, v...)
}
func Debugf(format string, v ...interface{}) {
defaultLogger.Debugf(format, v...)
}
func Infof(format string, v ...interface{}) {
defaultLogger.Infof(format, v...)
}
func Warnf(format string, v ...interface{}) {
defaultLogger.Warnf(format, v...)
}
func Errorf(format string, v ...interface{}) {
defaultLogger.Errorf(format, v...)
}
func Fatalf(format string, v ...interface{}) {
defaultLogger.Fatalf(format, v...)
}
func (l *Logger) Printf(format string, v ...interface{}) {
l.infoLogger.Printf(format, v...)
}
func (l *Logger) Tracef(format string, v ...interface{}) {
if l.logLevel <= TraceLevel {
l.traceLogger.Printf(format, v...)
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.logLevel <= DebugLevel {
l.debugLogger.Printf(format, v...)
}
}
func (l *Logger) Infof(format string, v ...interface{}) {
if l.logLevel <= InfoLevel {
l.infoLogger.Printf(format, v...)
}
}
func (l *Logger) Warnf(format string, v ...interface{}) {
if l.logLevel <= WarnLevel {
l.warnLogger.Printf(format, v...)
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.logLevel <= ErrorLevel {
l.errorLogger.Printf(format, v...)
}
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.fatalLogger.Fatalf(format, v...)
}
func (l *Logger) Trace(v ...interface{}) {
if l.logLevel <= TraceLevel {
l.traceLogger.Println(v...)
}
}
func (l *Logger) Debug(v ...interface{}) {
if l.logLevel <= DebugLevel {
l.debugLogger.Println(v...)
}
}
func (l *Logger) Info(v ...interface{}) {
if l.logLevel <= InfoLevel {
l.infoLogger.Println(v...)
}
}
func (l *Logger) Warn(v ...interface{}) {
if l.logLevel <= WarnLevel {
l.warnLogger.Println(v...)
}
}
func (l *Logger) Error(v ...interface{}) {
if l.logLevel <= ErrorLevel {
l.errorLogger.Println(v...)
}
}
func (l *Logger) Fatal(v ...interface{}) {
l.fatalLogger.Fatal(v...)
}
// Parse the level string and return the corresponding LogLevel, if it doesn't recognize the level, return a string
func ParseLogLevel(level string) (LogLevel, error) {
switch level {
case "trace":
return TraceLevel, nil
case "debug":
return DebugLevel, nil
case "info":
return InfoLevel, nil
case "warn":
return WarnLevel, nil
case "error":
return ErrorLevel, nil
case "fatal":
return FatalLevel, nil
default:
return 0, fmt.Errorf("unknown log level: '%s'", level)
}
}
func (l *Logger) SetLevel(level LogLevel) {
l.logLevel = level
}
func SetLevel(level LogLevel) {
defaultLogger.SetLevel(level)
}