Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new promslog and promslog/flag packages to wrap log/slog #677

Merged
merged 1 commit into from
Aug 28, 2024

Commits on Aug 28, 2024

  1. feat: new promslog and promslog/flag packages to wrap log/slog

    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:
    
    ```go
    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:
    ```go
    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:
    - 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 <[email protected]>
    tjhop committed Aug 28, 2024
    Configuration menu
    Copy the full SHA
    6ea2584 View commit details
    Browse the repository at this point in the history