xlog

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package xlog provides a thin wrapper around log/slog.

Index

Constants

View Source
const ErrorKey = "error"

Key used to denote error values.

Variables

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

Convenience slog.Attr generators. Allows cluttering imports (no need to import both log/slog and xlog).

View Source
var ErrNilWriter = errors.New("invalid writer: nil")

Functions

func Error

func Error(err error) slog.Attr

Error constructs a first-class error log attribute.

Not to be confused with (xlog.Logger).Error() or (log/slog).Error(), which produce an error-level log message.

func ParseLevel

func ParseLevel(s string) (l slog.Level, err error)

ParseLevel tries to convert a (case-insensitive) string into a slog.Level. Accepted values are "debug", "info", "warn", "warning", "error" and "fatal". Other input will result in err not being nil.

Types

type ErrorValue

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

ErrorValue holds an error value.

func (ErrorValue) LogValue

func (err ErrorValue) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (ErrorValue) Value

func (err ErrorValue) Value() slog.Value

Value extracts the error message.

type Logger

type Logger interface {
	// Debug writes log messages with DEBUG severity.
	Debug(msg string, a ...slog.Attr)
	// Info writes log messages with INFO severity.
	Info(msg string, a ...slog.Attr)
	// Warn writes log messages with WARN severity.
	Warn(msg string, a ...slog.Attr)
	// Error writes log messages with ERROR severity.
	Error(msg string, a ...slog.Attr)
	// Fatal writes log messages with ERROR severity, and then
	// exits the whole program.
	Fatal(msg string, a ...slog.Attr)
	// With returns a Logger that includes the given attributes
	// in each output operation.
	With(a ...slog.Attr) Logger
}

A Logger allows writing messages with various severities.

func New

func New(opt ...Option) (Logger, error)

New creates a new logger instance. By default, log messages are written to stdout, and the log level is INFO.

func NewDiscard

func NewDiscard() Logger

NewDiscard produces basically the same logger as

xlog.New(xlog.Discard(), otheroptions...)

but without the option application overhead.

type Option

type Option func(*options) error

An Option represents a functional configuration option. These are used to configure new logger instances.

func AsJSON

func AsJSON() Option

AsJSON configures a JSONHandler, i.e. log messages will be printed as JSON string.

func AsText

func AsText() Option

AsText configures a TextHandler, i.e. the message output is a simple list of key=value pairs with minimal quoting.

func Color

func Color() Option

Color enables colorful log output. If the output writer set by WriteTo isn't a TTY, or messages are output AsJSON, enabling colors won't have an effect.

func Discard

func Discard() Option

Discard mutes the logger. See also NewDiscard() for a simpler constructor.

func Leveled

func Leveled(l slog.Level) Option

Leveled sets the log level.

func LeveledString

func LeveledString(s string) Option

LeveledString interprets s (see ParseLevel) and sets the log level. If s is unknown, the error will be revealed with New().

func MockClock

func MockClock(t time.Time) Option

MockClock sets up a canned timestamp.

func WithSource

func WithSource() Option

WithSource enables source code positions in log messages.

func WriteTo

func WriteTo(w io.Writer) Option

WriteTo sets the output.

Jump to

Keyboard shortcuts

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