Documentation ¶
Overview ¶
Package slogflag implements command-line flag parsing for slog.Level variables.
Usage ¶
Define flags using slogflag.Level. This declares a slog.Level flag, -log-level, stored in the pointer level, with type *slog.Level:
var level = slogflag.Level("log-level", slog.LevelInfo, "help message for flag -log-level")
If you like, you can bind the flag to a variable using the slogflag.LevelVar function:
var levelVar slog.Level func init() { slogflag.LevelVar(&levelVar, "log-level", slog.LevelInfo, "help message for flag -log-level") }
After all the flags are defined, call flag.Parse as usual. Then you can set the log level of your slog.Logger like this:
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: *level}))
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrParse = errors.New("cannot parse log level")
ErrParse is the error returned when it's impossible to parse the log level to something slog accepts.
Functions ¶
func Level ¶
Level defines a slog.Level flag with specified name, default value, and usage string. The return value is the address of a slog.Level variable that stores the value of the flag.
func LevelVar ¶
LevelVar defines a slog.Level flag with specified name, default value, and usage string. The argument p points to a slog.Level variable in which to store the value of the flag.
Types ¶
This section is empty.