Skip to content

Migrate logging system to log/slog #35122

@TheFox0x7

Description

@TheFox0x7

Feature Description

if accepted supersedes: #21758
filing as separate proposal due to a slightly different scope with the same outcome and names specific provider instead of just outcome.

With the introduction of go 1.21 log/slog was added to standard library. It's a structured logging library that allows to log a message with a key-value pairs added to it for later processing. It provides two formatted loggers (plus built-in default) json and logfmt

Why slog instead of zerolog or zap? It's in the standard library. I don't have a strong reason for/against alternative libraries.

As for why I feel it's worth migrating:

Benefits

  • It is very helpful when making statistics from logs with tools like angle-grinder, loki and similar tools
  • It's easier to filter through logs as you can set more complex filters to narrow the search down to specific context or exclude noise instead of just grepping through (though that doesn't go away).
  • Easy to include and find context for the log, for example when logging error in caused by a user, userid can be added as an attribute to look for later, which could be helpful for abuse detection via logs.
  • Third party extensions (such as sampling or some specific sinks) are available.
  • Specific loggers can write to the same file separated with an attribute

Drawbacks

  • Requires modifying all log lines in current codebase from printf to structured. For the switch current logs could format the message with sprintf so the change wouldn't have to be done in one go but in chunks.
  • I believe support for adding, pausing and removing loggers would require custom handler or io.Writer.
  • It's a breaking change unless a custom handler to print old style is added.
  • Access logs might be an issue to port (if they would be ported to this at all).

Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/proposalThe new feature has not been accepted yet but needs to be discussed first.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions