Skip to content

Use slog as default and only logger #1039

@iwpnd

Description

@iwpnd

Heya,

as discussed in #1032 and slack I would like to tackle the refactor from text based standard logger and zap structured logger to a single logger around golangs slog with the goal to simplify logging in Tegola, and moving logging closer to the standard libraries.

Status quo

  • The old implementation uses global variables (e.g., logger, level, IsError, etc.) and manual flag management for log level checks
  • even though we log structured logs, we do not use structured logging, we put a message in a structured log, that's it.

This approach was necessary 5 years ago, but fell out of time.

Migration stages

Stage 1

  • drop zap and std logger in favour of slog
  • add a tracelog to every Error log for easier debugging. We achieve this with a custom log handler that adds a stacktrace field and populates it. The custom handler will be passed to slog.SetDefault() and will become the global logger.
  • use slog in log.Info,log.Infof (...) to keep compatibility and reduce the changes in stage 1 to a minimum
  • follow slogs approach of Info, Debug, Warn and Error to keep things simple
  • Drop Fatal(f). Fatal(f) is really only an Error(f) with an os.Exit(1). The os.Exit(1) should be explicit, not implicitly hidden in a fatal log.
  • Drop Trace, it's not being used anyways
  • logs will go to os.Stderr instead of os.Stdout, use one output for simplicity.

Stage 2

  • drop log.Debug, log.Infof and the likes
  • modules will either import and use slog.Default(), that at this point is out custom logger,
  • or they receive the logger via dependency injection.
  • Module-specific loggers can be derived by adding context using With or WithGroup.

Can we agree on this? :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions