forked from go-playground/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.go
238 lines (201 loc) · 6.82 KB
/
pkg.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
package log
import (
"fmt"
"runtime"
"time"
)
// F creates a new field key + value entry
func F(key string, value interface{}) Field {
return Logger.F(key, value)
}
// Debug level formatted message.
func Debug(v ...interface{}) {
e := newEntry(DebugLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Trace starts a trace & returns Traceable object to End + log.
// Example defer log.Trace(...).End()
func Trace(v ...interface{}) Traceable {
t := Logger.tracePool.Get().(*TraceEntry)
t.entry = newEntry(TraceLevel, fmt.Sprint(v...), make([]Field, 0), skipLevel)
t.start = time.Now().UTC()
return t
}
// Info level formatted message.
func Info(v ...interface{}) {
e := newEntry(InfoLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Notice level formatted message.
func Notice(v ...interface{}) {
e := newEntry(NoticeLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Warn level formatted message.
func Warn(v ...interface{}) {
e := newEntry(WarnLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Error level formatted message.
func Error(v ...interface{}) {
e := newEntry(ErrorLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Panic logs an Panic level formatted message and then panics
// it is here to let this log package be a drop in replacement
// for the standard logger
func Panic(v ...interface{}) {
s := fmt.Sprint(v...)
e := newEntry(PanicLevel, s, nil, skipLevel)
Logger.HandleEntry(e)
panic(s)
}
// Alert level formatted message.
func Alert(v ...interface{}) {
s := fmt.Sprint(v...)
e := newEntry(AlertLevel, s, nil, skipLevel)
Logger.HandleEntry(e)
}
// Fatal level formatted message, followed by an exit.
func Fatal(v ...interface{}) {
e := newEntry(FatalLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
exitFunc(1)
}
// Fatalln level formatted message, followed by an exit.
func Fatalln(v ...interface{}) {
e := newEntry(FatalLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
exitFunc(1)
}
// Debugf level formatted message.
func Debugf(msg string, v ...interface{}) {
e := newEntry(DebugLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Tracef starts a trace & returns Traceable object to End + log
func Tracef(msg string, v ...interface{}) Traceable {
t := Logger.tracePool.Get().(*TraceEntry)
t.entry = newEntry(TraceLevel, fmt.Sprintf(msg, v...), make([]Field, 0), skipLevel)
t.start = time.Now().UTC()
return t
}
// Infof level formatted message.
func Infof(msg string, v ...interface{}) {
e := newEntry(InfoLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Noticef level formatted message.
func Noticef(msg string, v ...interface{}) {
e := newEntry(NoticeLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Warnf level formatted message.
func Warnf(msg string, v ...interface{}) {
e := newEntry(WarnLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Errorf level formatted message.
func Errorf(msg string, v ...interface{}) {
e := newEntry(ErrorLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Panicf logs an Panic level formatted message and then panics
// it is here to let this log package be a near drop in replacement
// for the standard logger
func Panicf(msg string, v ...interface{}) {
s := fmt.Sprintf(msg, v...)
e := newEntry(PanicLevel, s, nil, skipLevel)
Logger.HandleEntry(e)
panic(s)
}
// Alertf level formatted message.
func Alertf(msg string, v ...interface{}) {
s := fmt.Sprintf(msg, v...)
e := newEntry(AlertLevel, s, nil, skipLevel)
Logger.HandleEntry(e)
}
// Fatalf level formatted message, followed by an exit.
func Fatalf(msg string, v ...interface{}) {
e := newEntry(FatalLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
exitFunc(1)
}
// Panicln logs an Panic level formatted message and then panics
// it is here to let this log package be a near drop in replacement
// for the standard logger
func Panicln(v ...interface{}) {
s := fmt.Sprint(v...)
e := newEntry(PanicLevel, s, nil, skipLevel)
Logger.HandleEntry(e)
panic(s)
}
// Print logs an Info level formatted message
func Print(v ...interface{}) {
e := newEntry(InfoLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Println logs an Info level formatted message
func Println(v ...interface{}) {
e := newEntry(InfoLevel, fmt.Sprint(v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// Printf logs an Info level formatted message
func Printf(msg string, v ...interface{}) {
e := newEntry(InfoLevel, fmt.Sprintf(msg, v...), nil, skipLevel)
Logger.HandleEntry(e)
}
// WithFields returns a log Entry with fields set
func WithFields(fields ...Field) LeveledLogger {
return newEntry(InfoLevel, "", fields, skipLevel)
}
// StackTrace creates a new log Entry with pre-populated field with stack trace.
func StackTrace() LeveledLogger {
trace := make([]byte, 1<<16)
n := runtime.Stack(trace, true)
if n > stackTraceLimit {
n = stackTraceLimit
}
return newEntry(DebugLevel, "", []Field{F("stack trace", string(trace[:n])+"\n")}, skipLevel)
}
// HandleEntry send the logs entry out to all the registered handlers
func HandleEntry(e *Entry) {
Logger.HandleEntry(e)
}
// RegisterHandler adds a new Log Handler and specifies what log levels
// the handler will be passed log entries for
func RegisterHandler(handler Handler, levels ...Level) {
Logger.RegisterHandler(handler, levels...)
}
// RegisterDurationFunc registers a custom duration function for Trace events
func RegisterDurationFunc(fn DurationFormatFunc) {
Logger.RegisterDurationFunc(fn)
}
// SetTimeFormat sets the time format used for Trace events
func SetTimeFormat(format string) {
Logger.SetTimeFormat(format)
}
// SetCallerInfoLevels tells the logger to gather and set file and line number
// information on Entry objects for the provided log levels.
// By defaut all but TraceLevel, InfoLevel and NoticeLevel are set to gather information.
func SetCallerInfoLevels(levels ...Level) {
Logger.SetCallerInfoLevels(levels...)
}
// SetCallerSkipDiff adds the provided diff to the caller SkipLevel values.
// This is used when wrapping this library, you can set ths to increase the
// skip values passed to Caller that retrieves the file + line number info.
func SetCallerSkipDiff(diff uint8) {
Logger.SetCallerSkipDiff(diff)
}
// HasHandlers returns if any handlers have been registered.
func HasHandlers() bool {
return Logger.HasHandlers()
}
// SetApplicationID tells the logger to set a constant application key
// that will be set on all log Entry objects. log does not care what it is,
// the application name, app name + hostname.... that's up to you
// it is needed by many logging platforms for separating logs by application
// and even by application server in a distributed app.
func SetApplicationID(id string) {
Logger.SetApplicationID(id)
}