/
logger.go
70 lines (63 loc) · 1.75 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
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/curious-kitten/scratch-post/internal/info"
)
// Logger interface exposes the necessary logger functions
type Logger interface {
Info(args ...interface{})
Infof(msg string, args ...interface{})
Infow(msg string, keysAndValues ...interface{})
Debug(args ...interface{})
Debugf(msg string, args ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Error(args ...interface{})
Errorf(msg string, args ...interface{})
Errorw(msg string, keysAndValues ...interface{})
Fatal(args ...interface{})
}
// New returns a new Logger instance with the desired fields
func New(app info.App, instance info.Instance, debug bool) (Logger, func() error, error) {
initFields := map[string]interface{}{
"app": app.Name,
"version": app.Version,
"instance": instance.Hostname,
}
level := zapcore.InfoLevel
if debug {
level = zapcore.DebugLevel
}
zLogger, err := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(level),
DisableCaller: false,
DisableStacktrace: false,
OutputPaths: []string{"stdout"},
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
MessageKey: "msg",
},
InitialFields: initFields,
}.Build()
if err != nil {
return nil, nil, err
}
log := zLogger.Sugar()
return log, log.Sync, nil
}
// WithFields adds fields to the provided logger instance
func WithFields(log Logger, fields map[string]interface{}) Logger {
args := make([]interface{}, len(fields)*2)
i := 0
for k, v := range fields {
args[i] = k
i++
args[i] = v
i++
}
return log.(*zap.SugaredLogger).With(args...)
}