slogx

package
v0.0.0-...-ccee039 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 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

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

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

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 New

func New(cfg Config) *slog.Logger

New creates a logger from a Config. It also sets the slog default.

func WithLogger

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

WithLogger attaches a logger to a context.

Types

type Config

type Config struct {
	Format Format
	Level  slog.Level
}

Config holds logger configuration.

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) UnmarshalText

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

Jump to

Keyboard shortcuts

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