-
Notifications
You must be signed in to change notification settings - Fork 83
/
logger.go
85 lines (72 loc) · 2.64 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package logger
import (
"fmt"
"os"
"time"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
pkgLogger logr.Logger = logr.Discard()
Verbosity int
)
// Init initializes the package logger. Repeat calls will overwrite the package logger which may
// result in unexpected behavior.
func Init(verbosityLevel int) error {
Verbosity = verbosityLevel
encoderCfg := zap.NewDevelopmentEncoderConfig()
encoderCfg.EncodeLevel = nil
encoderCfg.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {}
// Level 6 and above are used for debugging and we want a different log structure for debug
// logs.
if verbosityLevel >= 6 {
encoderCfg.EncodeLevel = func(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
// Because we use negated levels it is necessary to negate the level again so the
// output appears in a V0 format.
//
// See logrAtomicLevel().
enc.AppendString(fmt.Sprintf("V%d", -int(l)))
}
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
}
// Build the encoder and logger.
consoleEncoder := zapcore.NewConsoleEncoder(encoderCfg)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), logrAtomicLevel(verbosityLevel)),
)
logger := zap.New(core)
// Configure package state so the logger can be used by other packages.
pkgLogger = zapr.NewLogger(logger)
return nil
}
// logrAtomicLevel creates a zapcore.AtomicLevel compatible with go-logr.
func logrAtomicLevel(level int) zap.AtomicLevel {
// The go-logr wrapper uses custom Zap log levels. To represent this in Zap, its
// necessary to negate the level to circumvent Zap level constraints.
//
// See https://github.com/go-logr/zapr/blob/master/zapr.go#L50.
return zap.NewAtomicLevelAt(zapcore.Level(-level))
}
// Get returns the logger instance that has been previously set.
// If no logger has been set, it returns a null logger.
func Get() logr.Logger {
return pkgLogger
}
// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
// the log line. The key/value pairs can then be used to add additional
// variable information. The key/value pairs should alternate string
// keys and arbitrary values.
func Info(msg string, keysAndValues ...interface{}) {
Get().Info(msg, keysAndValues...)
}
// V returns an Logger value for a specific verbosity level, relative to
// this Logger. In other words, V values are additive. V higher verbosity
// level means a log message is less important. It's illegal to pass a log
// level less than zero.
func V(level int) logr.Logger {
return Get().V(level)
}