-
Notifications
You must be signed in to change notification settings - Fork 408
/
logger.go
96 lines (81 loc) · 2.54 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
86
87
88
89
90
91
92
93
94
95
96
package logger
import (
"context"
"os"
"github.com/pkg/errors"
"github.com/go-logr/zapr"
"github.com/kyma-project/kyma/common/logging/tracing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/klog/v2"
)
type Logger struct {
zapLogger *zap.SugaredLogger
}
/*
This function creates logger structure based on given format, atomicLevel and additional cores
AtomicLevel structure allows to change level dynamically
*/
func NewWithAtomicLevel(format Format, atomicLevel zap.AtomicLevel, additionalCores ...zapcore.Core) (*Logger, error) {
return new(format, atomicLevel, additionalCores...)
}
/*
This function creates logger structure based on given format, level and additional cores
*/
func New(format Format, level Level, additionalCores ...zapcore.Core) (*Logger, error) {
filterLevel, err := level.ToZapLevel()
if err != nil {
return nil, errors.Wrap(err, "while getting zap log level")
}
levelEnabler := zap.LevelEnablerFunc(func(incomingLevel zapcore.Level) bool {
return incomingLevel >= filterLevel
})
return new(format, levelEnabler, additionalCores...)
}
func new(format Format, levelEnabler zapcore.LevelEnabler, additionalCores ...zapcore.Core) (*Logger, error) {
encoder, err := format.ToZapEncoder()
if err != nil {
return nil, errors.Wrapf(err, "while getting encoding configuration for %s format", format)
}
defaultCore := zapcore.NewCore(
encoder,
zapcore.Lock(os.Stderr),
levelEnabler,
)
cores := append(additionalCores, defaultCore)
return &Logger{zap.New(zapcore.NewTee(cores...), zap.AddCaller()).Sugar()}, nil
}
func (l *Logger) WithTracing(ctx context.Context) *zap.SugaredLogger {
newLogger := *l
for key, val := range tracing.GetMetadata(ctx) {
newLogger.zapLogger = newLogger.zapLogger.With(key, val)
}
return newLogger.WithContext()
}
func (l *Logger) WithContext() *zap.SugaredLogger {
return l.zapLogger.With(zap.Namespace("context"))
}
/*
By default the Fatal Error log will be in json format, because it's production default.
*/
func LogFatalError(format string, args ...interface{}) error {
logger, err := New(JSON, ERROR)
if err != nil {
return errors.Wrap(err, "while getting Error Json Logger")
}
logger.zapLogger.Fatalf(format, args...)
return nil
}
/*
This function initialize klog which is used in k8s/go-client
*/
func InitKlog(log *Logger, level Level) error {
zaprLogger := zapr.NewLogger(log.WithContext().Desugar())
lvl, err := level.ToZapLevel()
if err != nil {
return errors.Wrap(err, "while getting zap log level")
}
zaprLogger.V((int)(lvl))
klog.SetLogger(zaprLogger)
return nil
}