-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
37 lines (28 loc) · 955 Bytes
/
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
package deej
import (
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewLogger provides a logger instance for the whole program
func NewLogger() (*zap.SugaredLogger, error) {
loggerConfig := zap.NewDevelopmentConfig()
// make it colorful
loggerConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
// make it readable
loggerConfig.EncoderConfig.EncodeCaller = nil
loggerConfig.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
loggerConfig.EncoderConfig.EncodeName = func(s string, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(fmt.Sprintf("%-27s", s))
}
logger, err := loggerConfig.Build()
if err != nil {
return nil, fmt.Errorf("create zap logger: %w", err)
}
// no reason not to use the sugared logger - it's fast enough for anything we're gonna do
sugar := logger.Sugar()
return sugar, nil
}