Comparing v0.56.0...v0.57.0 · prometheus/common (original) (raw)
Commits on Aug 28, 2024
- feat: new promslog and promslog/flag packages to wrap log/slog
Prereq for prometheus/prometheus#14355
This adds a newpromslog
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]. Thepromslog
package also makes use of anslog.LevelVar
to dynamically adjust the
level of an existing logger. Similar to the oldpromlog
package, we
provide means to log inlogfmt
andjson
output formats.
Creating a logger is similar to the old promlog package -- optionally
populate aConfig
struct, and then callNew()
with the config.
In order to dynamically adjust the logger's level, retain theConfig
struct as it's own variable to access theAllowedLevel.Set()
method,
which internally updates theAllowedLevel
'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
Configuration menu
Browse the repository at this point in the history