diff --git a/go.mod b/go.mod index a3e98a9c8e..3052bd09b0 100644 --- a/go.mod +++ b/go.mod @@ -94,6 +94,11 @@ require ( sigs.k8s.io/yaml v1.4.0 ) +require ( + github.com/go-logr/zerologr v1.2.3 // indirect + github.com/rs/zerolog v1.33.0 // indirect +) + require ( cloud.google.com/go v0.114.0 // indirect cloud.google.com/go/auth v0.5.1 // indirect diff --git a/go.sum b/go.sum index 79878f2613..2aa18220ab 100644 --- a/go.sum +++ b/go.sum @@ -359,6 +359,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ= github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg= +github.com/go-logr/zerologr v1.2.3 h1:up5N9vcH9Xck3jJkXzgyOxozT14R47IyDODz8LM1KSs= +github.com/go-logr/zerologr v1.2.3/go.mod h1:BxwGo7y5zgSHYR1BjbnHPyF/5ZjVKfKxAZANVu6E8Ho= github.com/go-openapi/analysis v0.23.0 h1:aGday7OWupfMs+LbmLZG4k0MYXIANxcuBTYUC03zFCU= github.com/go-openapi/analysis v0.23.0/go.mod h1:9mz9ZWaSlV8TvjQHLl2mUW2PbZtemkE8yA5v22ohupo= github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w= @@ -628,6 +630,7 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -755,6 +758,9 @@ github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= @@ -1077,6 +1083,7 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/pkg/logging/log.go b/pkg/logging/log.go index 9a7c18fe56..a5ac2120b9 100644 --- a/pkg/logging/log.go +++ b/pkg/logging/log.go @@ -8,11 +8,11 @@ import ( stdlog "log" "os" "strings" + "time" "github.com/go-logr/logr" - "github.com/go-logr/zapr" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "github.com/go-logr/zerologr" + "github.com/rs/zerolog" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -55,51 +55,63 @@ func InitFlags(flags *flag.FlagSet) { // Setup configures the logger with the supplied log format. // It returns an error if the JSON logger could not be initialized or passed logFormat is not recognized. func Setup(logFormat string, loggingTimestampFormat string, level int) error { - var zc zap.Config + var zerologLevel zerolog.Level + switch level { + case 6: + zerologLevel = zerolog.TraceLevel + case 5: + zerologLevel = zerolog.TraceLevel + case 4: + zerologLevel = zerolog.TraceLevel + case 3: + zerologLevel = zerolog.DebugLevel + case 2: + zerologLevel = zerolog.InfoLevel + case 1: + zerologLevel = zerolog.WarnLevel + default: + zerologLevel = zerolog.WarnLevel + } + + zerolog.SetGlobalLevel(zerologLevel) + var logger zerolog.Logger + switch logFormat { case TextFormat: - zc = zap.NewDevelopmentConfig() - zc.EncoderConfig.EncodeLevel = zapLevelEncoderText + output := zerolog.ConsoleWriter{Out: os.Stderr} + output.TimeFormat = resolveTimestampFormat(loggingTimestampFormat) + logger = zerolog.New(output).With().Timestamp().Logger() case JSONFormat: - zc = zap.NewProductionConfig() - zc.EncoderConfig.EncodeLevel = zapLevelEncoderJson + logger = zerolog.New(os.Stderr).With().Timestamp().Logger() default: return errors.New("log format not recognized, pass `text` for text mode or `json` to enable JSON logging") } - // configure the timestamp format - switch loggingTimestampFormat { - case ISO8601: - zc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - case RFC3339: - zc.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder - case MILLIS: - zc.EncoderConfig.EncodeTime = zapcore.EpochMillisTimeEncoder - case NANOS: - zc.EncoderConfig.EncodeTime = zapcore.EpochNanosTimeEncoder - case EPOCH: - zc.EncoderConfig.EncodeTime = zapcore.EpochTimeEncoder - case RFC3339NANO: - zc.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder - case "default": - zc.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder - default: - return errors.New("timestamp format not recognized, pass `iso8601` for ISO8601, `rfc3339` for RFC3339, `rfc3339nano` for RFC3339NANO, `millis` for Epoch Millis, `nanos` for Epoch Nanos, or omit the flag for the Unix Epoch timestamp format") - } - // Zap's levels get more and less verbose as the number gets smaller and higher respectively (DebugLevel is -1, InfoLevel is 0, WarnLevel is 1, and so on). - zc.Level = zap.NewAtomicLevelAt(zapcore.Level(-1 * level)) - // disable stacktrace - zc.DisableStacktrace = true - zapLog, err := zc.Build() - if err != nil { - return err - } - globalLog = zapr.NewLogger(zapLog) - // in json mode we configure klog and global logger to use zapr + + globalLog = zerologr.New(&logger) klog.SetLogger(globalLog.WithName("klog")) log.SetLogger(globalLog) return nil } +func resolveTimestampFormat(format string) string { + switch format { + case ISO8601: + return time.RFC3339 + case RFC3339: + return time.RFC3339 + case MILLIS: + return time.StampMilli + case NANOS: + return time.StampNano + case EPOCH: + return time.UnixDate + case RFC3339NANO: + return time.RFC3339Nano + default: + return time.RFC3339 + } +} + // GlobalLogger returns a logr.Logger as configured in main. func GlobalLogger() logr.Logger { return globalLog @@ -188,21 +200,3 @@ func (a *writerAdapter) Write(p []byte) (int, error) { func StdLogger(logger logr.Logger, prefix string) *stdlog.Logger { return stdlog.New(&writerAdapter{logger: logger}, prefix, stdlog.LstdFlags) } - -func zapLevelEncoderText(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(zapLevelToString(l)) -} - -func zapLevelEncoderJson(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(strings.ToLower(zapLevelToString(l))) -} - -func zapLevelToString(zapLevel zapcore.Level) string { - if zapLevel <= 0 && zapLevel >= -2 { - return "INFO" - } else if zapLevel <= -3 { - return "DEBUG" - } else { - return zapLevel.CapitalString() - } -}