slogx

package
v0.0.0-...-2c4037b Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package slogx wraps log/slog with convenience helpers so callers only need a single import for structured logging.

It re-exports the most commonly used slog types and functions, adds context-based logger passing, format configuration (JSON, pretty, text), and a small set of attribute helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	String   = slog.String
	Int      = slog.Int
	Int64    = slog.Int64
	Bool     = slog.Bool
	Float64  = slog.Float64
	Duration = slog.Duration
	Time     = slog.Time
	Any      = slog.Any
	Group    = slog.Group
)

Re-export common slog attribute constructors.

Functions

func Err

func Err(err error) slog.Attr

Err creates an slog.Attr for an error value, keyed as "err".

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext retrieves a logger from the context. Returns slog.Default() if none is set.

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger attaches a logger to a context.

Types

type Format

type Format int

Format selects the log output format.

const (
	FormatJSON Format = iota
	FormatPretty
	FormatText
)

func (Format) MarshalText

func (f Format) MarshalText() ([]byte, error)

func (*Format) Set

func (f *Format) Set(s string) error

func (Format) String

func (f Format) String() string

String and Set make Format a flag.Value; both delegate to MarshalText / UnmarshalText so the accepted values and output are consistent.

func (*Format) UnmarshalText

func (f *Format) UnmarshalText(text []byte) error

type Level

type Level slog.Level

Level is a named slog level that implements flag.Value and encoding.TextMarshaler / encoding.TextUnmarshaler, mirroring how Format works so callers can register it directly with flag parsing libraries.

It also implements slog.Leveler so it can be passed to slog.HandlerOptions.Level and similar interfaces.

const (
	LevelDebug Level = Level(slog.LevelDebug)
	LevelInfo  Level = Level(slog.LevelInfo)
	LevelWarn  Level = Level(slog.LevelWarn)
	LevelError Level = Level(slog.LevelError)
)

Re-export slog levels so callers don't need to import both packages.

func (Level) Level

func (l Level) Level() slog.Level

Level implements slog.Leveler.

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

func (*Level) Set

func (l *Level) Set(s string) error

func (Level) String

func (l Level) String() string

String and Set make Level a flag.Value; both delegate to MarshalText / UnmarshalText so the accepted values and output are consistent.

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) error

type Logger

type Logger = slog.Logger

Logger is a type alias for slog.Logger so callers that already import slogx for Level/Format don't need a second import just for the pointer type.

func New

func New(format Format, level Level) *Logger

New creates a logger for the given format and level. It also sets the slog default.

Jump to

Keyboard shortcuts

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