feat: new promslog and promslog/flag packages to wrap log/slog by tjhop · Pull Request #677 · prometheus/common (original) (raw)

Prereq for prometheus/prometheus#14355

This adds a new promslog package to create an opinionated slog logger
for use within the prometheus ecosystem. By default, logs are written at
info level and formatted to add a kv pair for the source caller. If
backwards compatibility for the literal output format of the logging is
required, logs can be styled in a format that is similar to how the
promlog package formatted it's go-kit/log output [1]. The promslog
package also makes use of an slog.LevelVar to dynamically adjust the
level of an existing logger. Similar to the old promlog package, we
provide means to log in logfmt and json output formats.

Creating a logger is similar to the old promlog package -- optionally
populate a Config struct, and then call New() with the config.

In order to dynamically adjust the logger's level, retain the Config
struct as it's own variable to access the AllowedLevel.Set() method,
which internally updates the AllowedLevel's slog.LevelVar to the
desired log level. Ex:

config := &promslog.Config{} // Retain as variable if you need to dynamically adjust log level logger := promslog.New(config) config.Level.Set("debug") logger.Debug("your message here", "hello", "world")

To use go-kit style log output:

config := &promslog.Config{Style: promslog.GoKitStyle} // Retain as variable if you need to dynamically adjust log level logger := promslog.New(config) config.Level.Set("debug") logger.Debug("your message here", "hello", "world")

  1. When using the go-kit log style, the following changes are made to the
    default slog output:

Signed-off-by: TJ Hoplock t.hoplock@gmail.com