slogflag

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 5 Imported by: 0

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

View Source
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

func Level(name string, value slog.Level, usage string) *slog.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

func LevelVar(p *slog.Level, name string, value slog.Level, usage string)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL