-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
295 lines (244 loc) · 5.58 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
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
package cog // import "github.com/reconquest/cog"
import (
"encoding/json"
"fmt"
"os"
"github.com/kovetskiy/lorg"
"github.com/reconquest/karma-go"
)
type (
Sender func(lorg.Level, karma.Hierarchical) error
Displayer func(lorg.Level, karma.Hierarchical)
Exiter func(int)
)
// Logger provides structured logging methods, based on karma package.
type Logger struct {
*lorg.Log
sender Sender
displayer Displayer
exiter Exiter
exitcode *int
}
func NewLogger(output *lorg.Log) *Logger {
logger := &Logger{Log: output}
output.SetExiter(logger.handleExit)
return logger
}
func (logger *Logger) NewChild() *Logger {
child := NewLogger(logger.Log.NewChild())
child.SetSender(logger.sender)
child.SetDisplayer(logger.displayer)
return child
}
func (logger *Logger) NewChildWithPrefix(prefix string) *Logger {
child := NewLogger(logger.Log.NewChildWithPrefix(prefix))
child.SetSender(logger.sender)
child.SetDisplayer(logger.displayer)
child.SetExiter(logger.exiter)
return child
}
// SetSender sets given function as callack for every log line
func (logger *Logger) SetSender(sender Sender) {
logger.sender = sender
}
func (logger *Logger) SetExiter(exiter func(int)) {
logger.exiter = exiter
}
// SetDisplayer sets given function as callack when displays log line
func (logger *Logger) SetDisplayer(display Displayer) {
logger.displayer = display
}
func (logger *Logger) handleExit(code int) {
logger.exitcode = &code
}
func (logger *Logger) Tracef(
context *karma.Context,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelTrace, context, message, args...)
}
func (logger *Logger) Debugf(
context *karma.Context,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelDebug, context, message, args...)
}
func (logger *Logger) Infof(
context *karma.Context,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelInfo, context, message, args...)
}
func (logger *Logger) Warningf(
err error,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelWarning, err, message, args...)
}
func (logger *Logger) Errorf(
err error,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelError, err, message, args...)
}
func (logger *Logger) Fatalf(
err error,
message string,
args ...interface{},
) {
logger.Write(lorg.LevelFatal, err, message, args...)
}
func (logger *Logger) Trace(
args ...interface{},
) {
logger.write(lorg.LevelTrace, args...)
}
func (logger *Logger) Debug(
args ...interface{},
) {
logger.write(lorg.LevelDebug, args...)
}
func (logger *Logger) Info(
args ...interface{},
) {
logger.write(lorg.LevelInfo, args...)
}
func (logger *Logger) Warning(
args ...interface{},
) {
logger.write(lorg.LevelWarning, args...)
}
func (logger *Logger) Error(
args ...interface{},
) {
logger.write(lorg.LevelError, args...)
}
func (logger *Logger) Fatal(
args ...interface{},
) {
logger.write(lorg.LevelFatal, args...)
}
func (logger *Logger) write(level lorg.Level, args ...interface{}) {
if len(args) == 0 {
return
}
if logger == nil {
return
}
var hierarchy karma.Hierarchical
arg0, isKarma := args[0].(karma.Hierarchical)
if isKarma {
hierarchy = arg0
}
if len(args) > 1 || !isKarma {
reason := hierarchy
var message string
if isKarma {
message = fmt.Sprint(args[1:]...)
} else {
message = fmt.Sprint(args...)
}
hierarchy = karma.Karma{
Reason: reason,
Message: message,
}
}
logger.displaySend(level, hierarchy)
}
func (logger *Logger) Write(
level lorg.Level,
reason interface{},
message string,
args ...interface{},
) {
if logger == nil {
return
}
var hierarchy karma.Hierarchical
switch reason := reason.(type) {
case karma.Hierarchical:
hierarchy = karma.Format(reason, message, args...)
case *karma.Context:
newHierarchy := karma.Format(nil, message, args...)
newHierarchy.Context = reason
hierarchy = newHierarchy
default:
hierarchy = karma.Format(reason, message, args...)
}
logger.displaySend(level, hierarchy)
}
func (logger *Logger) displaySend(
level lorg.Level,
hierarchy karma.Hierarchical,
) {
logger.Display(level, hierarchy)
err := logger.Send(level, hierarchy)
if err != nil {
logger.Display(
lorg.LevelError,
karma.Format(err, "error while sending log"),
)
}
if logger.exitcode != nil {
if logger.exiter != nil {
logger.exiter(*logger.exitcode)
} else {
os.Exit(*logger.exitcode)
}
}
}
func (logger *Logger) Display(level lorg.Level, hierarchy karma.Hierarchical) {
if logger.displayer != nil {
logger.displayer(level, hierarchy)
} else {
Display(logger, level, hierarchy)
}
}
func (logger *Logger) Send(
level lorg.Level,
hierarchy karma.Hierarchical,
) error {
if logger.sender != nil {
return logger.sender(level, hierarchy)
}
return nil
}
func Display(logger *Logger, level lorg.Level, hierarchy karma.Hierarchical) {
loggers := map[lorg.Level]func(...interface{}){
lorg.LevelTrace: logger.Log.Trace,
lorg.LevelDebug: logger.Log.Debug,
lorg.LevelInfo: logger.Log.Info,
lorg.LevelWarning: logger.Log.Warning,
lorg.LevelError: logger.Log.Error,
lorg.LevelFatal: logger.Log.Fatal,
}
log := loggers[level]
log(hierarchy.String())
}
func (logger *Logger) TraceJSON(obj interface{}) (encoded string) {
if logger.GetLevel() != lorg.LevelTrace {
return ""
}
defer func() {
err := recover()
if err != nil {
encoded = fmt.Sprintf(
"%#v (unable to encode to json: %s)",
obj, err,
)
}
}()
contents, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return fmt.Sprintf(
"%#v (unable to encode to json: %s)",
obj, err,
)
}
return string(contents)
}