Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
d80tb7 committed Jan 8, 2025
1 parent 7901e4b commit a834009
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/logging.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
console:
level: INFO
format: text
format: colorful
file:
enabled: true
level: INFO
Expand Down
59 changes: 38 additions & 21 deletions internal/common/logging/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package logging

import (
"fmt"

Check failure on line 4 in internal/common/logging/application.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

File is not `gofumpt`-ed (gofumpt)
"github.com/pkg/errors"

Check failure on line 5 in internal/common/logging/application.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

File is not `goimports`-ed with -local github.com/armadaproject/armada (goimports)
"github.com/rs/zerolog"
"gopkg.in/natefinch/lumberjack.v2"
"io"

Check failure on line 8 in internal/common/logging/application.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

File is not `gofumpt`-ed (gofumpt)
"os"
"path/filepath"
"sigs.k8s.io/yaml"
"strconv"

Check failure on line 12 in internal/common/logging/application.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

File is not `gofumpt`-ed (gofumpt)
"strings"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"gopkg.in/natefinch/lumberjack.v2"
"sigs.k8s.io/yaml"
)

var (
Expand All @@ -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
Expand Down Expand Up @@ -73,42 +71,61 @@ 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) {
level, err := zerolog.ParseLevel(logConfig.Console.Level)
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))
},
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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/common/logging/pulsar_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a834009

Please sign in to comment.