-
Notifications
You must be signed in to change notification settings - Fork 53
/
encoder.go
44 lines (40 loc) · 1.16 KB
/
encoder.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
package util
import (
"strings"
"time"
"github.com/go-logr/logr"
"github.com/onsi/ginkgo/v2"
"github.com/ttacon/chalk"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var EncoderConfig = zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
FunctionKey: "",
StacktraceKey: "S",
ConsoleSeparator: " ",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeCaller: func(ec zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(chalk.Dim.TextStyle(ec.TrimmedPath()))
},
EncodeName: func(name string, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(chalk.Dim.TextStyle(
strings.Replace(name, "controller-runtime.manager.", "", 1)))
},
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("[15:04:05]"))
},
EncodeDuration: zapcore.SecondsDurationEncoder,
}
func NewTestLogger() logr.Logger {
return zap.New(
zap.Level(zapcore.InfoLevel),
zap.WriteTo(ginkgo.GinkgoWriter),
zap.UseDevMode(false),
zap.Encoder(zapcore.NewConsoleEncoder(EncoderConfig)),
)
}