Documentation
¶
Index ¶
- Constants
- Variables
- func NewHandler(w io.Writer, fmt LogFormatValue, lvl slog.Level) slog.Handler
- func NewLogFormatFlag(f LogFormatValue) *flag.Flag
- func NewLogLevelFlag(l *slog.Level) *flag.Flag
- type LogFlags
- func (f *LogFlags) Adapter(w io.Writer, opts *slog.HandlerOptions) *slogadapter.SlogAdapter
- func (f *LogFlags) FormatFlag() *flag.Flag
- func (f *LogFlags) Handler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func (f *LogFlags) Level() slog.Level
- func (f *LogFlags) LevelFlag() *flag.Flag
- func (f *LogFlags) ParseEnv() error
- func (f *LogFlags) ParseFunc(get func(string) string) error
- type LogFormatValue
Constants ¶
const ( FlagUsageLevel = "Log level. Valid values include debug, info, warn, and error" FlagUsageFormat = "Log format. Valid values include text, and json" )
const ( FlagLogLevel = "log.level" FlagLogFormat = "log.format" )
const ( EnvLogLevel = "LOG_LEVEL" EnvLogFormat = "LOG_FORMAT" )
Variables ¶
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
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.
Types ¶
type LogFlags ¶
type LogFlags struct {
// contains filtered or unexported fields
}
LogFlags is a data storage for flag.FlagSet parsing results.
func NewEnvLogFlags ¶
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 ¶
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
FormatFlag returns a flag.Flag controlling the instance LogFormat.
func (*LogFlags) 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) LevelFlag ¶ added in v0.4.0
LevelFlag returns a flag.Flag controlling the instance LogLevel.
func (*LogFlags) ParseEnv ¶
ParseEnv calls LogFlags.ParseFunc with os.Getenv.
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.