-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggie.go
264 lines (217 loc) · 5.82 KB
/
loggie.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Package loggie provides simple unstructured leveled logging.
package loggie // import "syreclabs.com/go/loggie"
import (
"fmt"
"io"
"os"
"runtime"
"sync"
colorable "github.com/mattn/go-colorable"
isatty "github.com/mattn/go-isatty"
)
// Known logging levels. Setting logging level to Lnone silences all logging output
// (including Fatal and Panic logging).
const (
Ldebug = iota
Linfo
Lwarning
Lerror
Lfatal
Lpanic
Lnone
)
// Logger is the logging interface.
type Logger interface {
Print(lvl int, v ...interface{})
Printf(lvl int, format string, v ...interface{})
Debug(v ...interface{})
Debugf(format string, v ...interface{})
Info(v ...interface{})
Infof(format string, v ...interface{})
Warning(v ...interface{})
Warningf(format string, v ...interface{})
Error(v ...interface{})
Errorf(format string, v ...interface{})
Panic(v ...interface{})
Panicf(format string, v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
Level() int
SetLevel(lvl int)
}
type defaultLogger struct {
name string
level int
f Formatter
mu sync.Mutex
w io.Writer
}
// NewLogger creates a new named logger writing to w and using formatter f.
func NewLogger(w io.Writer, name string, f Formatter) Logger {
return &defaultLogger{w: w, name: name, f: f}
}
// New returns new named os.Stdout logger with default text formatter.
func New(name string) Logger {
return NewLogger(os.Stdout, name, DefaultFormatter)
}
func (l *defaultLogger) Print(lvl int, v ...interface{}) {
if lvl >= l.level {
l.write(lvl, v...)
if lvl > Lwarning {
l.writeStack(lvl)
}
}
}
func (l *defaultLogger) Printf(lvl int, format string, v ...interface{}) {
if lvl >= l.level {
l.writef(lvl, format, v...)
if lvl > Lwarning {
l.writeStack(lvl)
}
}
}
func (l *defaultLogger) Debug(v ...interface{}) {
l.Print(Ldebug, v...)
}
func (l *defaultLogger) Debugf(format string, v ...interface{}) {
l.Printf(Ldebug, format, v...)
}
func (l *defaultLogger) Info(v ...interface{}) {
l.Print(Linfo, v...)
}
func (l *defaultLogger) Infof(format string, v ...interface{}) {
l.Printf(Linfo, format, v...)
}
func (l *defaultLogger) Warning(v ...interface{}) {
l.Print(Lwarning, v...)
}
func (l *defaultLogger) Warningf(format string, v ...interface{}) {
l.Printf(Lwarning, format, v...)
}
func (l *defaultLogger) Error(v ...interface{}) {
l.Print(Lerror, v...)
}
func (l *defaultLogger) Errorf(format string, v ...interface{}) {
l.Printf(Lerror, format, v...)
}
// Panic logs with Lpanic level and calls panic().
func (l *defaultLogger) Panic(v ...interface{}) {
var msg string
if l.level <= Lpanic {
msg, _ = l.write(Lpanic, v...)
}
panic(msg)
}
// Panicf logs with Lpanic level and calls panic().
func (l *defaultLogger) Panicf(format string, v ...interface{}) {
var msg string
if l.level <= Lpanic {
msg, _ = l.writef(Lpanic, format, v...)
}
panic(msg)
}
// Fatal logs with Lfatal level and calls os.Exit(1).
func (l *defaultLogger) Fatal(v ...interface{}) {
l.Print(Lfatal, v...)
os.Exit(1)
}
// Fatalf logs with Lfatal level and calls os.Exit(1).
func (l *defaultLogger) Fatalf(format string, v ...interface{}) {
l.Printf(Lfatal, format, v...)
os.Exit(1)
}
// Level returns current minimal logging level.
func (l *defaultLogger) Level() int {
return l.level
}
// SetLevel sets minimal output level. All logging with levels below lvl will
// be silenced.
func (l *defaultLogger) SetLevel(lvl int) {
l.level = lvl
}
func (l *defaultLogger) write(lvl int, v ...interface{}) (string, error) {
l.mu.Lock()
defer l.mu.Unlock()
msg := fmt.Sprintln(v...)
return msg, l.f.Format(l.w, lvl, l.name, msg)
}
func (l *defaultLogger) writef(lvl int, format string, v ...interface{}) (string, error) {
l.mu.Lock()
defer l.mu.Unlock()
msg := fmt.Sprintf(format, v...)
return msg, l.f.Format(l.w, lvl, l.name, msg)
}
func (l *defaultLogger) writeStack(lvl int) {
stack := make([]byte, 1024*8)
stack = stack[:runtime.Stack(stack, false)]
l.w.Write(stack)
}
var Default Logger
func Print(lvl int, v ...interface{}) {
Default.Print(lvl, v...)
}
func Printf(lvl int, format string, v ...interface{}) {
Default.Printf(lvl, format, v...)
}
func Debug(v ...interface{}) {
Default.Debug(v...)
}
func Debugf(format string, v ...interface{}) {
Default.Debugf(format, v...)
}
func Info(v ...interface{}) {
Default.Debug(v...)
}
func Infof(format string, v ...interface{}) {
Default.Debugf(format, v...)
}
func Warning(v ...interface{}) {
Default.Debug(v...)
}
func Warningf(format string, v ...interface{}) {
Default.Debugf(format, v...)
}
func Error(v ...interface{}) {
Default.Debug(v...)
}
func Errorf(format string, v ...interface{}) {
Default.Debugf(format, v...)
}
func Panic(v ...interface{}) {
Default.Panic(v...)
}
func Panicf(format string, v ...interface{}) {
Default.Panicf(format, v...)
}
func Fatal(v ...interface{}) {
Default.Fatal(v...)
}
func Fatalf(format string, v ...interface{}) {
Default.Fatalf(format, v...)
}
func Level() int {
return Default.Level()
}
func SetLevel(lvl int) {
Default.SetLevel(lvl)
}
// Default formatter is default output formatter.
// It produces colorized output if output destination is a tty and plain text otherwise.
// Colorization additionally can be controller by CLICOLOR and CLICOLOR_FORCE environment
// variables: CLICOLOR=0 disables colorization and CLICOLOR_FORCE=1 forces it even when
// output is not a tty.
var DefaultFormatter Formatter
func init() {
isAtty := isatty.IsTerminal(os.Stdout.Fd())
clicolor := os.Getenv("CLICOLOR")
clicolorForce := os.Getenv("CLICOLOR_FORCE")
var writer io.Writer
if (isAtty || clicolorForce == "1") && clicolor != "0" {
writer = colorable.NewColorableStdout()
DefaultFormatter = NewTextFormatter(Fdefault | Fcolor)
} else {
writer = os.Stdout
DefaultFormatter = NewTextFormatter(Fdefault)
}
Default = NewLogger(writer, "", DefaultFormatter)
}