forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslog.go
222 lines (167 loc) · 5.2 KB
/
slog.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
/*
Package slog Lightweight, extensible, configurable logging library written in Go.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/slog
Quick usage:
package main
import (
"github.com/gookit/slog"
)
func main() {
slog.Info("info log message")
slog.Warn("warning log message")
slog.Infof("info log %s", "message")
slog.Debugf("debug %s", "message")
}
More usage please see README.
*/
package slog
import (
"time"
)
// var bufferPool *sync.Pool
func init() {
// bufferPool = &sync.Pool{
// New: func() interface{} {
// return new(bytes.Buffer)
// },
// }
}
//
// ------------------------------------------------------------
// Global std logger operate
// ------------------------------------------------------------
//
// std logger is an SugaredLogger.
// It is directly available without any additional configuration
var std = NewStdLogger()
// Std get std logger
func Std() *SugaredLogger { return std }
// Reset the std logger
func Reset() {
ResetExitHandlers(true)
// new std
std = NewStdLogger()
}
// Configure the std logger
func Configure(fn func(l *SugaredLogger)) { std.Configure(fn) }
// Exit runs all the logger exit handlers and then terminates the program using os.Exit(code)
func Exit(code int) { std.Exit(code) }
// SetExitFunc to the std logger
func SetExitFunc(fn func(code int)) { std.ExitFunc = fn }
// Flush log messages
func Flush() error { return std.Flush() }
// MustFlush log messages
func MustFlush() {
err := Flush()
if err != nil {
panic(err)
}
}
// FlushTimeout flush logs with timeout.
func FlushTimeout(timeout time.Duration) { std.FlushTimeout(timeout) }
// FlushDaemon run flush handle on daemon
//
// Usage:
// go slog.FlushDaemon()
func FlushDaemon() { std.FlushDaemon() }
// SetLogLevel for the std logger
func SetLogLevel(l Level) { std.Level = l }
// SetFormatter to std logger
func SetFormatter(f Formatter) { std.Formatter = f }
// GetFormatter of the std logger
func GetFormatter() Formatter { return std.Formatter }
// AddHandler to the std logger
func AddHandler(h Handler) { std.AddHandler(h) }
// PushHandler to the std logger
func PushHandler(h Handler) { std.AddHandler(h) }
// AddHandlers to the std logger
func AddHandlers(hs ...Handler) { std.AddHandlers(hs...) }
// PushHandlers to the std logger
func PushHandlers(hs ...Handler) { std.PushHandlers(hs...) }
// AddProcessor to the logger
func AddProcessor(p Processor) { std.AddProcessor(p) }
// AddProcessors to the logger
func AddProcessors(ps ...Processor) { std.AddProcessors(ps...) }
// -------------------------- New record with log data, fields -----------------------------
// WithData new record with data
func WithData(data M) *Record {
return std.WithData(data)
}
// WithFields new record with fields
func WithFields(fields M) *Record {
return std.WithFields(fields)
}
// -------------------------- Add log messages with level -----------------------------
// Print logs a message at level PrintLevel
func Print(args ...interface{}) { std.log(PrintLevel, args) }
// Println logs a message at level PrintLevel
func Println(args ...interface{}) { std.log(PrintLevel, args) }
// Printf logs a message at level PrintLevel
func Printf(format string, args ...interface{}) { std.logf(PrintLevel, format, args) }
// Trace logs a message at level Trace
func Trace(args ...interface{}) { std.log(TraceLevel, args) }
// Tracef logs a message at level Trace
func Tracef(format string, args ...interface{}) { std.logf(TraceLevel, format, args) }
// Info logs a message at level Info
func Info(args ...interface{}) {
std.log(InfoLevel, args)
}
// Infof logs a message at level Info
func Infof(format string, args ...interface{}) {
std.logf(InfoLevel, format, args)
}
// Notice logs a message at level Notice
func Notice(args ...interface{}) {
std.log(NoticeLevel, args)
}
// Noticef logs a message at level Notice
func Noticef(format string, args ...interface{}) {
std.logf(NoticeLevel, format, args)
}
// Warn logs a message at level Warn
func Warn(args ...interface{}) {
std.log(WarnLevel, args)
}
// Warnf logs a message at level Warn
func Warnf(format string, args ...interface{}) {
std.logf(WarnLevel, format, args)
}
// Error logs a message at level Error
func Error(args ...interface{}) {
std.log(ErrorLevel, args)
}
// ErrorT logs a error type at level Error
func ErrorT(err error) {
if err != nil {
std.log(ErrorLevel, []interface{}{err})
}
}
// Errorf logs a message at level Error
func Errorf(format string, args ...interface{}) {
std.logf(ErrorLevel, format, args)
}
// Debug logs a message at level Debug
func Debug(args ...interface{}) {
std.log(DebugLevel, args)
}
// Debugf logs a message at level Debug
func Debugf(format string, args ...interface{}) {
std.logf(DebugLevel, format, args)
}
// Fatal logs a message at level Fatal
func Fatal(args ...interface{}) {
std.log(FatalLevel, args)
}
// Fatalf logs a message at level Fatal
func Fatalf(format string, args ...interface{}) {
std.logf(FatalLevel, format, args)
}
// Panic logs a message at level Panic
func Panic(args ...interface{}) {
std.log(PanicLevel, args)
}
// Panicf logs a message at level Panic
func Panicf(format string, args ...interface{}) {
std.logf(PanicLevel, format, args)
}