-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
235 lines (193 loc) · 5.45 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
package log
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
)
// Logger is the interface for outputing log messages in different levels.
// A new Logger can be created with NewLogger() function.
// You can changed the output handler with SetHandler() function.
type Logger interface {
// SetLevel changes the level of the logger. Default is logging.Info.
SetLevel(Level)
// SetHandler replaces the current handler for output. Default is logging.StderrHandler.
SetHandler(Handler)
// SetCallDepth sets the parameter passed to runtime.Caller().
// It is used to get the file name from call stack.
// For example you need to set it to 1 if you are using a wrapper around
// the Logger. Default value is zero.
SetCallDepth(int)
// Fatal is equivalent to Logger.Critical followed by a call to os.Exit(1).
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Fatalln(args ...interface{})
// Panic is equivalent to Logger.Critical followed by a call to panic().
Panic(args ...interface{})
Panicf(format string, args ...interface{})
Panicln(args ...interface{})
// Log functions
Critical(args ...interface{})
Criticalf(format string, args ...interface{})
Criticalln(args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Errorln(args ...interface{})
Warning(args ...interface{})
Warningf(format string, args ...interface{})
Warningln(args ...interface{})
Notice(args ...interface{})
Noticef(format string, args ...interface{})
Noticeln(args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Infoln(args ...interface{})
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Debugln(args ...interface{})
}
///////////////////////////
// //
// Logger implementation //
// //
///////////////////////////
// logger is the default Logger implementation.
type logger struct {
Name string
Level Level
Handler Handler
calldepth int
}
// NewLogger returns a new Logger implementation. Do not forget to close it at exit.
func NewLogger(name string) Logger {
return &logger{
Name: name,
Level: DefaultLevel,
Handler: DefaultHandler,
}
}
func (l *logger) SetLevel(level Level) { l.Level = level }
func (l *logger) SetHandler(b Handler) { l.Handler = b }
func (l *logger) SetCallDepth(n int) { l.calldepth = n }
func (l *logger) log(level Level, args ...interface{}) {
if level > l.Level {
return
}
l.logMsg(level, fmt.Sprint(args...))
}
func (l *logger) logf(level Level, format string, args ...interface{}) {
if level > l.Level {
return
}
l.logMsg(level, fmt.Sprintf(format, args...))
}
func (l *logger) logln(level Level, args ...interface{}) {
if level > l.Level {
return
}
l.logMsg(level, fmt.Sprintln(args...))
}
func (l *logger) logMsg(level Level, message string) {
if level > l.Level {
return
}
_, file, line, ok := runtime.Caller(l.calldepth + 2)
if !ok {
file = "???"
line = 0
}
rec := &Record{
Message: message,
LoggerName: l.Name,
Level: level,
Time: time.Now(),
Filename: file,
Line: line,
ProcessName: procName,
ProcessID: pid,
}
l.Handler.Handle(rec)
}
// procName returns the name of the current process.
// func procName() string { return filepath.Base(os.Args[0]) }
var procName = filepath.Base(os.Args[0])
var pid = os.Getpid()
func (l *logger) Fatal(args ...interface{}) {
l.log(CRITICAL, args...)
os.Exit(1)
}
func (l *logger) Fatalf(format string, args ...interface{}) {
l.logf(CRITICAL, format, args...)
os.Exit(1)
}
func (l *logger) Fatalln(args ...interface{}) {
l.logln(CRITICAL, args...)
os.Exit(1)
}
func (l *logger) Panic(args ...interface{}) {
l.log(CRITICAL, args...)
panic(fmt.Sprint(args...))
}
func (l *logger) Panicf(format string, args ...interface{}) {
l.logf(CRITICAL, format, args...)
panic(fmt.Sprintf(format, args...))
}
func (l *logger) Panicln(args ...interface{}) {
l.logln(CRITICAL, args...)
panic(fmt.Sprintln(args...))
}
func (l *logger) Critical(args ...interface{}) {
l.log(CRITICAL, args...)
}
func (l *logger) Criticalf(format string, args ...interface{}) {
l.logf(CRITICAL, format, args...)
}
func (l *logger) Criticalln(args ...interface{}) {
l.logln(CRITICAL, args...)
}
func (l *logger) Error(args ...interface{}) {
l.log(ERROR, args...)
}
func (l *logger) Errorf(format string, args ...interface{}) {
l.logf(ERROR, format, args...)
}
func (l *logger) Errorln(args ...interface{}) {
l.logln(ERROR, args...)
}
func (l *logger) Warning(args ...interface{}) {
l.log(WARNING, args...)
}
func (l *logger) Warningf(format string, args ...interface{}) {
l.logf(WARNING, format, args...)
}
func (l *logger) Warningln(args ...interface{}) {
l.logln(WARNING, args...)
}
func (l *logger) Notice(args ...interface{}) {
l.log(NOTICE, args...)
}
func (l *logger) Noticef(format string, args ...interface{}) {
l.logf(NOTICE, format, args...)
}
func (l *logger) Noticeln(args ...interface{}) {
l.logln(NOTICE, args...)
}
func (l *logger) Info(args ...interface{}) {
l.log(INFO, args...)
}
func (l *logger) Infof(format string, args ...interface{}) {
l.logf(INFO, format, args...)
}
func (l *logger) Infoln(args ...interface{}) {
l.logln(INFO, args...)
}
func (l *logger) Debug(args ...interface{}) {
l.log(DEBUG, args...)
}
func (l *logger) Debugf(format string, args ...interface{}) {
l.logf(DEBUG, format, args...)
}
func (l *logger) Debugln(args ...interface{}) {
l.logln(DEBUG, args...)
}