From a83400905768d5de7e0ce4d744aef6a03d55ca65 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 18:28:12 +0000 Subject: [PATCH] fixes --- config/logging.yaml | 2 +- internal/common/logging/application.go | 59 +++++++++++++++-------- internal/common/logging/pulsar_adapter.go | 2 +- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/config/logging.yaml b/config/logging.yaml index 30f04cac185..3c7eeab868c 100644 --- a/config/logging.yaml +++ b/config/logging.yaml @@ -1,6 +1,6 @@ console: level: INFO - format: text + format: colorful file: enabled: true level: INFO diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 146ef62b5bc..279dbbc174d 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,17 +2,15 @@ package logging import ( "fmt" + "github.com/pkg/errors" + "github.com/rs/zerolog" + "gopkg.in/natefinch/lumberjack.v2" "io" "os" "path/filepath" + "sigs.k8s.io/yaml" "strconv" "strings" - "time" - - "github.com/pkg/errors" - "github.com/rs/zerolog" - "gopkg.in/natefinch/lumberjack.v2" - "sigs.k8s.io/yaml" ) var ( @@ -36,7 +34,7 @@ func MustConfigureApplicationLogging() { // a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. func ConfigureApplicationLogging() error { // Set some global logging properties - zerolog.TimeFieldFormat = time.RFC3339Nano // needs to be higher or greater precision than the writer format. + zerolog.TimeFieldFormat = RFC3339Milli // needs to be higher or greater precision than the writer format. zerolog.CallerMarshalFunc = shortCallerEncoder // Load config file @@ -73,21 +71,25 @@ func ConfigureApplicationLogging() error { } func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { - level, err := zerolog.ParseLevel(logConfig.Console.Level) + level, err := zerolog.ParseLevel(logConfig.File.Level) if err != nil { return nil, err } - return &FilteredLevelWriter{ - level: level, - writer: &lumberjack.Logger{ - Filename: logConfig.File.LogFile, - MaxSize: logConfig.File.Rotation.MaxSizeMb, - MaxBackups: logConfig.File.Rotation.MaxBackups, - MaxAge: logConfig.File.Rotation.MaxAgeDays, - Compress: logConfig.File.Rotation.Compress, - }, - }, nil + // Set up lumberjack for log rotation + lumberjackLogger := &lumberjack.Logger{ + Filename: logConfig.File.LogFile, + MaxSize: logConfig.File.Rotation.MaxSizeMb, + MaxBackups: logConfig.File.Rotation.MaxBackups, + MaxAge: logConfig.File.Rotation.MaxAgeDays, + Compress: logConfig.File.Rotation.Compress, + } + + if strings.ToLower(logConfig.File.Format) == "text" || strings.ToLower(logConfig.File.Format) == "colorful" { + return createConsoleWriter(lumberjackLogger, level, logConfig.File.Format), nil + } else { + return createJsonWriter(lumberjackLogger, level), nil + } } func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { @@ -95,10 +97,25 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } + if strings.ToLower(logConfig.Console.Format) == "text" || strings.ToLower(logConfig.Console.Format) == "colorful" { + return createConsoleWriter(os.Stdout, level, logConfig.Console.Format), nil + } else { + return createJsonWriter(os.Stdout, level), nil + } +} + +func createJsonWriter(out io.Writer, level zerolog.Level) *FilteredLevelWriter { + return &FilteredLevelWriter{ + level: level, + writer: out, + } +} + +func createConsoleWriter(out io.Writer, level zerolog.Level, format string) *FilteredLevelWriter { return &FilteredLevelWriter{ level: level, writer: zerolog.ConsoleWriter{ - Out: os.Stdout, + Out: out, TimeFormat: RFC3339Milli, FormatLevel: func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) @@ -106,9 +123,9 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { FormatCaller: func(i interface{}) string { return filepath.Base(fmt.Sprintf("%s", i)) }, - NoColor: true, + NoColor: strings.ToLower(format) == "text", }, - }, nil + } } func readConfig(configFilePath string) (Config, error) { diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go index 1ff8490dad7..d4e13d04b59 100644 --- a/internal/common/logging/pulsar_adapter.go +++ b/internal/common/logging/pulsar_adapter.go @@ -4,7 +4,7 @@ import ( pulsarlog "github.com/apache/pulsar-client-go/pulsar/log" ) -var pulsarAdapterSkipFrames = StdSkipFrames + 1 +var pulsarAdapterSkipFrames = StdSkipFrames // Wrapper to adapt Logger to the logger interface expected by the pulsar client type pulsarWrapper struct {