-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.go
78 lines (64 loc) · 2.24 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
package handel
import (
"os"
"github.com/go-kit/kit/log"
// conflicts with handel.level type
lvl "github.com/go-kit/kit/log/level"
)
// Logger is a interface that can log to different levels. Handel calls these
// methods with key-value pairs as in structured logging framework do.
type Logger interface {
Info(keyvals ...interface{})
Debug(keyvals ...interface{})
Warn(keyvals ...interface{})
Error(keyvals ...interface{})
// With returns a new Logger that inserts the given key value pairs for each
// statements at each levels
With(keyvals ...interface{}) Logger
}
// DefaultLevel is the default level where statements are logged. Change the
// value of this variable before init() to change the level of the default
// logger.
var DefaultLevel = lvl.AllowInfo()
// DefaultLogger is the default logger that only statemetns at the
// default level. The level is set by DefaultLevel inside init()..
var DefaultLogger Logger
func init() {
DefaultLogger = NewKitLogger(DefaultLevel)
}
type kitLogger struct {
log.Logger
}
// NewKitLoggerFrom returns a Logger out of a go-kit/kit/log logger interface. The
// caller can set the options that it needs to the logger first. By default, it
// wraps the logger with a SyncLogger since Handel is highly concurrent.
func NewKitLoggerFrom(l log.Logger) Logger {
return &kitLogger{log.NewSyncLogger(l)}
}
// NewKitLogger returns a Logger based on go-kit/kit/log default logger
// structure that outputs to stdout. You can pass in options to only allow
// certain levels. By default, it also includes the caller stack.
func NewKitLogger(opts ...lvl.Option) Logger {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
for _, opt := range opts {
logger = lvl.NewFilter(logger, opt)
}
logger = log.With(logger, "call", log.Caller(8))
return NewKitLoggerFrom(logger)
}
func (k *kitLogger) Info(kv ...interface{}) {
lvl.Info(k.Logger).Log(kv...)
}
func (k *kitLogger) Debug(kv ...interface{}) {
lvl.Debug(k.Logger).Log(kv...)
}
func (k *kitLogger) Warn(kv ...interface{}) {
lvl.Warn(k.Logger).Log(kv...)
}
func (k *kitLogger) Error(kv ...interface{}) {
lvl.Error(k.Logger).Log(kv...)
}
func (k *kitLogger) With(kv ...interface{}) Logger {
newLogger := log.With(k.Logger, kv...)
return NewKitLoggerFrom(newLogger)
}