-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.go
66 lines (54 loc) · 1.55 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
package log
import (
"fmt"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var log logr.Logger
var config zap.Config
const (
DebugLevel = 1
InfoLevel = 0
)
func init() {
config = zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(zapcore.Level(0))
// Make the logging human readable
config.Encoding = "console"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
// Build the logger
zap, err := config.Build()
if err != nil {
panic(fmt.Sprintf("initializing logger (%v)?", err))
}
log = zapr.NewLogger(zap)
}
func EnableDebug() {
config.Level = zap.NewAtomicLevelAt(zapcore.Level(-1))
zap, err := config.Build()
if err != nil {
panic(fmt.Sprintf("enabling debug on logger (%v)?", err))
}
log = zapr.NewLogger(zap)
}
func Info(msg string, keysAndValues ...any) {
log.WithCallDepth(1).V(InfoLevel).Info(msg, keysAndValues...)
}
func Infof(format string, values ...any) {
log.WithCallDepth(1).V(InfoLevel).Info(fmt.Sprintf(format, values...))
}
func Debug(msg string, keysAndValues ...any) {
log.WithCallDepth(1).V(DebugLevel).Info(msg, keysAndValues...)
}
func Debugf(format string, values ...any) {
log.WithCallDepth(1).V(DebugLevel).Info(fmt.Sprintf(format, values...))
}
func Error(err error, msg string, keysAndValues ...any) {
log.WithCallDepth(1).Error(err, msg, keysAndValues...)
}
func Errorf(err error, format string, values ...any) {
log.WithCallDepth(1).Error(err, fmt.Sprintf(format, values...))
}