-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
259 lines (222 loc) · 5.53 KB
/
log.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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
package ecslog
import (
"context"
"fmt"
"strconv"
"github.com/urso/diag"
"github.com/urso/diag/ctxfmt"
"github.com/urso/ecslog/backend"
)
type Logger struct {
ctx *diag.Context
name string
backend backend.Backend
}
type Level = backend.Level
const (
Trace Level = backend.Trace
Debug Level = backend.Debug
Info Level = backend.Info
Error Level = backend.Error
)
func New(backend backend.Backend) *Logger {
return &Logger{
ctx: diag.NewContext(nil, nil),
name: "",
backend: backend,
}
}
func (l *Logger) IsEnabled(lvl Level) bool {
return l.backend.IsEnabled(lvl)
}
func (l *Logger) Named(name string) *Logger {
return &Logger{
ctx: diag.NewContext(l.ctx, nil),
backend: l.backend.For(name),
name: name,
}
}
func (l *Logger) With(args ...interface{}) *Logger {
nl := &Logger{
ctx: diag.NewContext(l.ctx, nil),
backend: l.backend,
}
nl.ctx.AddAll(args...)
return nl
}
func (l *Logger) WithFields(fields ...diag.Field) *Logger {
nl := &Logger{
ctx: diag.NewContext(l.ctx, nil),
backend: l.backend,
}
nl.ctx.AddFields(fields...)
return nl
}
func (l *Logger) WithDiagnosticContext(ctx *diag.Context) *Logger {
if ctx.Len() == 0 {
return l.With()
}
var merged *diag.Context
if l.ctx.Len() == 0 {
merged = ctx
} else {
merged = diag.NewContext(l.ctx, ctx)
}
return &Logger{
ctx: diag.NewContext(merged, nil),
backend: l.backend,
}
}
func (l *Logger) WithDiagnotics(ctx context.Context) *Logger {
dc, _ := diag.DiagnosticsFrom(ctx)
if dc.Len() == 0 {
return l.With()
}
return l.WithDiagnosticContext(dc)
}
func (l *Logger) Trace(args ...interface{}) { l.log(Trace, 1, args) }
func (l *Logger) Tracef(msg string, args ...interface{}) { l.logf(Trace, 1, msg, args) }
func (l *Logger) Debug(args ...interface{}) { l.log(Debug, 1, args) }
func (l *Logger) Debugf(msg string, args ...interface{}) { l.logf(Debug, 1, msg, args) }
func (l *Logger) Info(args ...interface{}) { l.log(Info, 1, args) }
func (l *Logger) Infof(msg string, args ...interface{}) { l.logf(Info, 1, msg, args) }
func (l *Logger) Error(args ...interface{}) { l.log(Error, 1, args) }
func (l *Logger) Errorf(msg string, args ...interface{}) { l.logf(Error, 1, msg, args) }
func (l *Logger) log(lvl Level, skip int, args []interface{}) {
if !l.IsEnabled(lvl) {
return
}
if l.backend.UseContext() {
l.logArgsCtx(lvl, skip+1, args)
} else {
l.logArgs(lvl, skip+1, args)
}
}
func (l *Logger) logf(lvl Level, skip int, msg string, args []interface{}) {
if !l.IsEnabled(lvl) {
return
}
if l.backend.UseContext() {
l.logfMsgCtx(lvl, skip+1, msg, args)
} else {
l.logfMsg(lvl, skip+1, msg, args)
}
}
func (l *Logger) logArgsCtx(lvl Level, skip int, args []interface{}) {
msg := argsMessage(args)
ctx := diag.NewContext(l.ctx, nil)
var causes []error
for _, arg := range args {
switch v := arg.(type) {
case diag.Field:
ctx.AddField(v)
case error:
causes = append(causes, v)
}
}
l.backend.Log(backend.Message{
Name: l.name,
Level: lvl,
Caller: getCaller(skip + 1),
Message: msg,
Context: ctx,
Causes: causes,
})
}
func (l *Logger) logArgs(lvl Level, skip int, args []interface{}) {
msg := argsMessage(args)
var causes []error
for _, arg := range args {
if err, ok := arg.(error); ok {
causes = append(causes, err)
}
}
l.backend.Log(backend.Message{
Name: l.name,
Level: lvl,
Caller: getCaller(skip + 1),
Message: msg,
Context: diag.NewContext(nil, nil),
Causes: causes,
})
}
func argsMessage(args []interface{}) string {
if len(args) == 0 {
return ""
}
if len(args) == 1 {
if str, ok := args[0].(string); ok {
return str
}
}
return fmt.Sprint(args...)
}
func (l *Logger) logfMsgCtx(lvl Level, skip int, msg string, args []interface{}) {
ctx := diag.NewContext(l.ctx, nil)
var causes []error
msg, rest := ctxfmt.Sprintf(func(key string, idx int, val interface{}) {
if field, ok := (val).(diag.Field); ok {
if key != "" {
ctx.Add(fmt.Sprintf("%v.%v", key, field.Key), field.Value)
} else {
ctx.AddField(field)
}
return
}
switch v := val.(type) {
case diag.Value:
ctx.Add(ensureKey(key, idx), v)
case error:
causes = append(causes, v)
if key != "" {
ctx.AddField(diag.String(key, v.Error()))
}
default:
ctx.AddField(diag.Any(ensureKey(key, idx), val))
}
}, msg, args...)
if len(rest) > 0 {
msg = fmt.Sprintf("%s {EXTRA_FIELDS: %v}", msg, rest)
}
l.backend.Log(backend.Message{
Name: l.name,
Level: lvl,
Caller: getCaller(skip + 1),
Message: msg,
Context: ctx,
Causes: causes,
})
}
func (l *Logger) logfMsg(lvl Level, skip int, msg string, args []interface{}) {
var causes []error
msg, rest := ctxfmt.Sprintf(func(key string, idx int, val interface{}) {
if err, ok := val.(error); ok {
causes = append(causes, err)
}
}, msg, args...)
if len(rest) > 0 {
msg = fmt.Sprintf("%s {EXTRA_FIELDS: %v}", msg, rest)
}
l.backend.Log(backend.Message{
Name: l.name,
Level: lvl,
Caller: getCaller(skip + 1),
Message: msg,
Context: diag.NewContext(nil, nil),
Causes: causes,
})
}
func ensureKey(key string, idx int) string {
if key == "" {
return strconv.FormatInt(int64(idx), 10)
}
return key
}
func getCaller(skip int) backend.Caller {
return backend.GetCaller(skip + 1)
}