stdflags

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2025 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	FlagUsageLevel  = "Log level. Valid values include debug, info, warn, and error"
	FlagUsageFormat = "Log format. Valid values include text, and json"
)
View Source
const (
	FlagLogLevel  = "log.level"
	FlagLogFormat = "log.format"
)
View Source
const (
	EnvLogLevel  = "LOG_LEVEL"
	EnvLogFormat = "LOG_FORMAT"
)

Variables

View Source
var ErrInvalidFormat = errors.New("unsupported log format")

ErrInvalidFormat is the error returned during unmarshalling of log format values.

Functions

func NewHandler added in v0.4.0

func NewHandler(w io.Writer, fmt LogFormatValue, lvl slog.Level) slog.Handler

NewHandler create a [slog.Handler] whose constructor options are only initialized with the provided level. for more complex scenarios use [LogFormatValue.Handler] directly.

func NewLogFormatFlag added in v0.4.0

func NewLogFormatFlag(f LogFormatValue) *flag.Flag

NewLogFormatValue returns a flag.Flag instance wrapping the provided format value. It is primarily intended for integration with other logging frameworks.

func NewLogLevelFlag added in v0.4.0

func NewLogLevelFlag(l *slog.Level) *flag.Flag

NewLogLevelFlag returns a flag.Flag instance wrapping the provided level pointer. It is primarily intended for integration with other logging frameworks.

Types

type LogFlags

type LogFlags struct {
	// contains filtered or unexported fields
}

LogFlags is a data storage for flag.FlagSet parsing results.

func NewEnvLogFlags

func NewEnvLogFlags(fs *flag.FlagSet, prefix string) *LogFlags

NewEnvLogFlags returns a LogFlags instance with the given flagset primed for populating its internal state. he flags usage description will include mentions of environment variables. If this is not desired, use NewLogFlags.

The environment variables will use the optional prefix as-is.

func NewLogFlags

func NewLogFlags(fs *flag.FlagSet) *LogFlags

NewLogFlags returns a LogFlags instance with the given flagset primed for populating its internal state.

func (*LogFlags) Adapter

func (f *LogFlags) Adapter(w io.Writer, opts *slog.HandlerOptions) *slogadapter.SlogAdapter

Adapter returns a [slogadapter.SlogAdapter] with a newly created [slog.Logger]. The provided handler options are optional.

func (*LogFlags) FormatFlag added in v0.4.0

func (f *LogFlags) FormatFlag() *flag.Flag

FormatFlag returns a flag.Flag controlling the instance LogFormat.

func (*LogFlags) Handler

func (f *LogFlags) Handler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

Handler returns a [slog.Handler] from the internal state. If no handler options are provided, a default set will be created. In either case, the level field will be overwritten with the value of LogFlags.Level.

func (*LogFlags) Level

func (f *LogFlags) Level() slog.Level

Level returns the parsed level.

func (*LogFlags) LevelFlag added in v0.4.0

func (f *LogFlags) LevelFlag() *flag.Flag

LevelFlag returns a flag.Flag controlling the instance LogLevel.

func (*LogFlags) ParseEnv

func (f *LogFlags) ParseEnv() error

ParseEnv calls LogFlags.ParseFunc with os.Getenv.

func (*LogFlags) ParseFunc

func (f *LogFlags) ParseFunc(get func(string) string) error

ParseFunc uses the provided function to retrieve values for the previously provisioned flagset. Values are only forwarded if they are not empty. Returned errors originate from the flag parsing logic.

type LogFormatValue

type LogFormatValue interface {
	flag.Value
	encoding.TextMarshaler
	encoding.TextUnmarshaler

	// Handler returns a [slog.Handler] instance based on the represented format.
	// See [slog.NewJSONHandler] and [slog.NewTextHandler] for information about
	// parameter handling.
	Handler(io.Writer, *slog.HandlerOptions) slog.Handler
}

LogFormatValue combines various interfaces for use with commandline flag parsing

func LogFormatJSON

func LogFormatJSON() LogFormatValue

LogFormatJSON represents the slog.JSONHandler.

func LogFormatText

func LogFormatText() LogFormatValue

LogFormatText represents the slog.TextHandler.

Jump to

Keyboard shortcuts

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