-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
68 lines (56 loc) · 1.71 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
package nvelope
import (
"fmt"
)
// BasicLogger is just the start of what a logger might
// support. It exists mostly as a placeholder. Future
// versions of nvelope will prefer more capabile loggers
// but will use type assertions so that the BasicLogger
// will remain acceptable to the APIs.
type BasicLogger interface {
Debug(msg string, fields ...map[string]interface{})
Error(msg string, fields ...map[string]interface{})
Warn(msg string, fields ...map[string]interface{})
}
// StdLogger is implmented by the base library log.Logger
type StdLogger interface {
Print(v ...interface{})
}
type wrappedStdLogger struct {
log StdLogger
}
// LoggerFromStd creates a
func LoggerFromStd(log StdLogger) func() BasicLogger {
return func() BasicLogger {
return wrappedStdLogger{log: log}
}
}
func (std wrappedStdLogger) Error(msg string, fields ...map[string]interface{}) {
if len(fields) == 0 {
std.log.Print(msg)
return
}
vals := make([]interface{}, 1, len(fields)*4+1)
vals[0] = msg
for _, m := range fields {
for k, v := range m {
vals = append(vals, k+"="+fmt.Sprint(v))
}
}
std.log.Print(vals...)
}
func (std wrappedStdLogger) Warn(msg string, fields ...map[string]interface{}) {
std.Error(msg, fields...)
}
func (std wrappedStdLogger) Debug(msg string, fields ...map[string]interface{}) {
std.Error(msg, fields...)
}
// NoLogger injects a BasicLogger that discards all inputs
func NoLogger() BasicLogger {
return nilLogger{}
}
type nilLogger struct{}
var _ BasicLogger = nilLogger{}
func (nilLogger) Error(msg string, fields ...map[string]interface{}) {}
func (nilLogger) Warn(msg string, fields ...map[string]interface{}) {}
func (nilLogger) Debug(msg string, fields ...map[string]interface{}) {}