-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger.go
353 lines (289 loc) · 7.72 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package astilog
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/asticode/go-astikit"
)
var newLine = []byte("\n")
// Logger represents an object that can log stuff
type Logger struct {
c Configuration
createdAt time.Time
f formatter
fs map[string]interface{}
mf *sync.RWMutex // Locks fs
l astikit.LoggerLevel // Level
w io.WriteCloser
}
// NewFromFlags creates a new Logger based on flags
func NewFromFlags() (l *Logger) {
return New(FlagConfig())
}
var now = func() time.Time { return time.Now() }
// New creates a new Logger
func New(c Configuration) (l *Logger) {
// Create
l = &Logger{
c: c,
createdAt: now(),
fs: make(map[string]interface{}),
mf: &sync.RWMutex{},
}
// Add app name field
if c.AppName != "" {
l.WithField("app_name", c.AppName)
}
// Set writer
l.setWriter(c)
// Set level
l.setLevel(c)
// Set formatter
l.setFormatter(c, l.createdAt)
return
}
// Close closes the logger properly
func (l *Logger) Close() error {
return l.w.Close()
}
func (l *Logger) setWriter(c Configuration) {
// File
if c.Filename != "" {
// Open file
f, err := os.OpenFile(c.Filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
if err == nil {
l.w = f
return
}
// Revert to default
c.Out = ""
log.Println(fmt.Errorf("astilog: creating %s failed: %w", c.Filename, err))
}
// Syslog
if c.Out == OutSyslog {
// Create
s, err := newSyslogWriter(c)
if err == nil {
l.w = s
return
}
// Revert to default
c.Out = ""
log.Println(fmt.Errorf("astilog: creating syslog failed: %w", err))
}
// Stderr
if c.Out == OutStderr {
l.w = astikit.NopCloser(os.Stderr)
return
}
// Default is stdout
l.w = astikit.NopCloser(os.Stdout)
}
func (l *Logger) setLevel(c Configuration) {
l.l = c.Level
}
func (l *Logger) setFormatter(c Configuration, createdAt time.Time) {
switch c.Format {
case FormatJSON:
l.f = newJSONFormatter(c, createdAt)
case FormatMinimalist:
l.f = newMinimalistFormatter()
default:
l.f = newTextFormatter(c, createdAt)
}
}
func source() string {
// Skip self callers
i := 0
_, file, line, ok := runtime.Caller(i)
for ok && strings.Contains(file, "/go-astilog/logger.go") {
i++
_, file, line, ok = runtime.Caller(i)
}
// Process file
if !ok {
file = "<???>"
line = 1
} else {
file = filepath.Base(file)
}
return file + ":" + strconv.Itoa(line)
}
func (l *Logger) write(ctx context.Context, msgFunc func() string, lvl astikit.LoggerLevel) {
// Check level
if l.l > lvl {
return
}
// Create fields
fs := make(map[string]interface{})
l.mf.RLock()
for k, v := range l.fs {
fs[k] = v
}
l.mf.RUnlock()
// Add source
if l.c.Source {
fs["source"] = source()
}
// Add context fields
if cfs := fieldsFromContext(ctx); cfs != nil {
cfs.m.Lock()
for k, v := range cfs.fs {
fs[k] = v
}
cfs.m.Unlock()
}
// Format message
m := l.f.format(msgFunc(), lvl, fs)
// Write
if l.c.MaxWriteLength > 0 && len(m) > l.c.MaxWriteLength {
// Loop
var c int
for {
// Get boundaries
from := c * l.c.MaxWriteLength
to := (c + 1) * l.c.MaxWriteLength
// We're done
if from > len(m)-1 {
break
}
// We've reached the end of the message
if to > len(m) {
to = len(m)
}
// Since append modifies the input slice, we need to create a new one when
// appending a new line
wm := m[from:to]
if to != len(m) {
wm = make([]byte, to-from)
copy(wm, m[from:to])
if !bytes.HasSuffix(wm, newLine) {
wm = append(wm, newLine...)
}
}
// Write
if _, err := l.w.Write(wm); err != nil {
log.Println(fmt.Errorf("astilog: writing failed: %w", err))
return
}
// Increment
c++
}
} else {
// Write
if _, err := l.w.Write(m); err != nil {
log.Println(fmt.Errorf("astilog: writing failed: %w", err))
return
}
}
}
func msgFunc(v ...interface{}) func() string {
return func() string { return fmt.Sprint(v...) }
}
func msgFuncf(format string, v ...interface{}) func() string {
return func() string { return fmt.Sprintf(format, v...) }
}
func (l *Logger) Print(v ...interface{}) {
l.Info(v...)
}
func (l *Logger) Printf(format string, v ...interface{}) {
l.Infof(format, v...)
}
func (l *Logger) Debug(v ...interface{}) {
l.write(context.Background(), msgFunc(v...), astikit.LoggerLevelDebug)
}
func (l *Logger) DebugC(ctx context.Context, v ...interface{}) {
l.write(ctx, msgFunc(v...), astikit.LoggerLevelDebug)
}
func (l *Logger) DebugCf(ctx context.Context, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), astikit.LoggerLevelDebug)
}
func (l *Logger) Debugf(format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), astikit.LoggerLevelDebug)
}
func (l *Logger) Info(v ...interface{}) {
l.write(context.Background(), msgFunc(v...), astikit.LoggerLevelInfo)
}
func (l *Logger) InfoC(ctx context.Context, v ...interface{}) {
l.write(ctx, msgFunc(v...), astikit.LoggerLevelInfo)
}
func (l *Logger) InfoCf(ctx context.Context, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), astikit.LoggerLevelInfo)
}
func (l *Logger) Infof(format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), astikit.LoggerLevelInfo)
}
func (l *Logger) Warn(v ...interface{}) {
l.write(context.Background(), msgFunc(v...), astikit.LoggerLevelWarn)
}
func (l *Logger) WarnC(ctx context.Context, v ...interface{}) {
l.write(ctx, msgFunc(v...), astikit.LoggerLevelWarn)
}
func (l *Logger) WarnCf(ctx context.Context, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), astikit.LoggerLevelWarn)
}
func (l *Logger) Warnf(format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), astikit.LoggerLevelWarn)
}
func (l *Logger) Error(v ...interface{}) {
l.write(context.Background(), msgFunc(v...), astikit.LoggerLevelError)
}
func (l *Logger) ErrorC(ctx context.Context, v ...interface{}) {
l.write(ctx, msgFunc(v...), astikit.LoggerLevelError)
}
func (l *Logger) ErrorCf(ctx context.Context, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), astikit.LoggerLevelError)
}
func (l *Logger) Errorf(format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), astikit.LoggerLevelError)
}
var exit = func() { os.Exit(1) }
func (l *Logger) Fatal(v ...interface{}) {
l.write(context.Background(), msgFunc(v...), astikit.LoggerLevelFatal)
exit()
}
func (l *Logger) FatalC(ctx context.Context, v ...interface{}) {
l.write(ctx, msgFunc(v...), astikit.LoggerLevelFatal)
exit()
}
func (l *Logger) FatalCf(ctx context.Context, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), astikit.LoggerLevelFatal)
exit()
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), astikit.LoggerLevelFatal)
exit()
}
func (l *Logger) Write(lv astikit.LoggerLevel, v ...interface{}) {
l.write(context.Background(), msgFunc(v...), lv)
}
func (l *Logger) WriteC(ctx context.Context, lv astikit.LoggerLevel, v ...interface{}) {
l.write(ctx, msgFunc(v...), lv)
}
func (l *Logger) WriteCf(ctx context.Context, lv astikit.LoggerLevel, format string, v ...interface{}) {
l.write(ctx, msgFuncf(format, v...), lv)
}
func (l *Logger) Writef(lv astikit.LoggerLevel, format string, v ...interface{}) {
l.write(context.Background(), msgFuncf(format, v...), lv)
}
// WithField adds a field to the logger
func (l *Logger) WithField(k string, v interface{}) {
l.mf.Lock()
l.fs[k] = v
l.mf.Unlock()
}
// WithFields adds fields to the logger
func (l *Logger) WithFields(fs map[string]interface{}) {
for k, v := range fs {
l.WithField(k, v)
}
}