-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
55 lines (49 loc) · 1.21 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
package logger
import (
"context"
"os"
"slices"
"strconv"
"github.com/go-logr/logr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/klog/v2"
)
// New return a new logr.Logger and its associated flush function.
func New(ctx context.Context, cfg zapcore.EncoderConfig, writers ...zapcore.WriteSyncer) (logr.Logger, func(), error) {
f := defaultFile()
if f.Filename != "" {
writers = append(writers, f)
}
if stdout {
if !slices.ContainsFunc(writers, func(syncer zapcore.WriteSyncer) bool {
if syncer == os.Stdout {
return true
}
return false
}) {
writers = append(writers, os.Stdout)
}
}
if stderr {
if !slices.ContainsFunc(writers, func(syncer zapcore.WriteSyncer) bool {
if syncer == os.Stderr {
return true
}
return false
}) {
writers = append(writers, os.Stderr)
}
}
combinedWriteSyncer := zap.CombineWriteSyncers(writers...)
writer = combinedWriteSyncer
encoder := zapcore.NewConsoleEncoder(cfg)
if format == jsonLogFormat {
encoder = zapcore.NewJSONEncoder(cfg)
}
klog.InitFlags(klogFS)
if err := klogFS.Set("v", strconv.Itoa(klogV)); err != nil {
return logr.Logger{}, nil, err
}
return newLogger(ctx, zapcore.Level(-enab), encoder, combinedWriteSyncer)
}