Skip to content

Commit

Permalink
New logger for the Traefik logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 21, 2022
1 parent 27c02b5 commit 56f7515
Show file tree
Hide file tree
Showing 297 changed files with 2,337 additions and 1,934 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -162,7 +162,7 @@ issues:
text: "Function 'buildConstructor' has too many statements"
linters:
- funlen
- path: pkg/tracing/haystack/logger.go
- path: pkg/logs/haystack.go
linters:
- goprintffuncname
- path: pkg/tracing/tracing.go
Expand Down
90 changes: 90 additions & 0 deletions cmd/traefik/logger.go
@@ -0,0 +1,90 @@
package main

import (
"io"
stdlog "log"
"os"
"strings"
"time"

"github.com/natefinch/lumberjack"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sirupsen/logrus"
"github.com/traefik/traefik/v2/pkg/config/static"
"github.com/traefik/traefik/v2/pkg/logs"
)

func init() {
// hide the first logs before the setup of the logger.
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}

func setupLogger(staticConfiguration *static.Configuration) {
// configure log format
w := getLogWriter(staticConfiguration)

// configure log level
logLevel := getLogLevel(staticConfiguration)

zerolog.SetGlobalLevel(logLevel)

// create logger
logCtx := zerolog.New(w).With().Timestamp()
if logLevel <= zerolog.DebugLevel {
logCtx = logCtx.Caller()
}

log.Logger = logCtx.Logger()
zerolog.DefaultContextLogger = &log.Logger

// Global logrus replacement (related to lib like go-rancher-metadata, docker, etc.)
logrus.StandardLogger().Out = logs.NoLevel(log.Logger, zerolog.DebugLevel)

// configure default standard log.
stdlog.SetFlags(stdlog.Lshortfile | stdlog.LstdFlags)
stdlog.SetOutput(logs.NoLevel(log.Logger, zerolog.DebugLevel))
}

func getLogWriter(staticConfiguration *static.Configuration) io.Writer {
var w io.Writer = os.Stderr

if staticConfiguration.Log != nil && len(staticConfiguration.Log.FilePath) > 0 {
_, _ = os.Create(staticConfiguration.Log.FilePath)
w = &lumberjack.Logger{
Filename: staticConfiguration.Log.FilePath,
MaxSize: staticConfiguration.Log.MaxSize,
MaxBackups: staticConfiguration.Log.MaxBackups,
MaxAge: staticConfiguration.Log.MaxAge,
Compress: true,
}
}

if staticConfiguration.Log == nil || staticConfiguration.Log.Format != "json" {
w = zerolog.ConsoleWriter{
Out: w,
TimeFormat: time.RFC3339,
NoColor: staticConfiguration.Log != nil && (staticConfiguration.Log.NoColor || len(staticConfiguration.Log.FilePath) > 0),
}
}

return w
}

func getLogLevel(staticConfiguration *static.Configuration) zerolog.Level {
levelStr := "error"
if staticConfiguration.Log != nil && staticConfiguration.Log.Level != "" {
levelStr = strings.ToLower(staticConfiguration.Log.Level)
}

logLevel, err := zerolog.ParseLevel(strings.ToLower(levelStr))
if err != nil {
log.Error().Err(err).
Str("logLevel", levelStr).
Msg("Unspecified or invalid log level, setting the level to default (ERROR)...")

logLevel = zerolog.ErrorLevel
}

return logLevel
}

0 comments on commit 56f7515

Please sign in to comment.