Repository navigation
Expand file tree
/
Copy pathlogger.go
More file actions
59 lines (49 loc) · 1.74 KB
/
Copy pathlogger.go
File metadata and controls
59 lines (49 loc) · 1.74 KB
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
package logging
import (
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
)
// NewLogger creates a preconfigured global logger and configures the global zap logger
func NewLogger(cfg Config, metricsNamespace string) *zap.Logger {
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
// Parse log level text to zap.LogLevel. Error check isn't required because the input is already validated.
level := zap.NewAtomicLevel()
_ = level.UnmarshalText([]byte(cfg.Level))
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
level,
)
core = zapcore.RegisterHooks(core, prometheusHook(metricsNamespace))
logger := zap.New(core)
zap.ReplaceGlobals(logger)
return logger
}
// prometheusHook is a hook for the zap library which exposes Prometheus counters for various log levels.
func prometheusHook(metricsNamespace string) func(zapcore.Entry) error {
messageCounterVec := promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "log_messages_total",
Help: "Total number of log messages by log level emitted by KMinion.",
}, []string{"level"})
// Initialize counters for all supported log levels so that they expose 0 for each level on startup
supportedLevels := []zapcore.Level{
zapcore.DebugLevel,
zapcore.InfoLevel,
zapcore.WarnLevel,
zapcore.ErrorLevel,
zapcore.FatalLevel,
zapcore.PanicLevel,
}
for _, level := range supportedLevels {
messageCounterVec.WithLabelValues(level.String())
}
return func(entry zapcore.Entry) error {
messageCounterVec.WithLabelValues(entry.Level.String()).Inc()
return nil
}
}