pkg.go.dev

Package slogflags provides flags to configure log/slog.

Basic usage

Simply call flag.Parse and then call Logger to obtain a configured slog instance. Two new flags will be available to users of your app: `--log.level` which accepts a textual level ("debug", "info", "warn" or "error") and `--log.format` which accepts either "text" or "json".

flag.Parse()
logger := slogflags.Logger()
logger.Warn("This is not a drill", "key", "value", "etc", "etc)

Custom levels

If you define your own log levels, you can pass them to Logger using WithCustomLevels. Users can then specify them in the `log.level` flag.

Setting as the default logger

Pass WithSetDefault(true) when calling Logger to register the new instance as the default logger for log/slog. You can then call log/slog.Warn etc directly.

Redirecting old log calls

If your code or libraries use log rather than log/slog you can redirect them by setting a default logger as above. All log calls will be given the same log level, which you can alter using WithOldLogLevel. e.g.:

flag.Parse()
_ = slogflags.Logger(slogflags.WithSetDefault(true), slogflags.WithOldLogLevel(slog.LevelWarn))
log.Printf("hi")
// Prints: time=... level=WARN msg=hi

Other advanced usage

You can customise other behaviour of the created logger using WithDefaultLogLevel, WithWriter, WithAddSource and WithReplaceAttr. See the documentation for those funcs for more details.

This section is empty.

This section is empty.

Logger creates a new log/slog.Logger configured according to the options and flags.

flag.Parse must be called prior to calling this method.

type Option func(*config)

WithCustomLevels adds extra levels to the defaults available in the `log.level` flag. The same level may be specified with multiple different keys to provide aliases.

The "level" attribute of log messages will automatically be rewritten if the level matches a custom attribute. If multiple aliases are defined for the same level, the alias that comes first lexicographically will be used.

WithDefaultLogLevel sets the default level that will be used if the `log.level` flag is not set. If not provided, the default is log/slog.LevelInfo.

WithWriter sets a custom writer to be used for the log output. Defaults to os.Stdout.

Read the original on pkg.go.dev ↗