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 atinfo
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 thepromlog
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")
- When using the
go-kit
log style, the following changes are made to the
default slog output:
- adjusts slog default timestamp key from
timestamp
->ts
- adjusts the timestamp value to use the same time format string
("2006-01-02T15:04:05.000Z07:00") - adjusts slog default sourcecode key from
source
->caller
- adjusts the formatting of the sourcecode values to trim paths with
filepath.Base()
. The formatting of the sourcecode value is similar
to the go-kit/log usage, with the addition of the source function in
parenthesis when debug logging is enabled.
Signed-off-by: TJ Hoplock t.hoplock@gmail.com