errslog

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package errslog provides utilities to use errors with the slog package: wrap an error with attributes (WrapAttrs) and a level (WrapLevel), and log it with Log or LoggerLog.

Example
err := errbase.New("error")
err = WrapAttrs(err, slog.Int("int", 123))
attrs := GetAttrs(err)
fmt.Println(attrs[0])
Output:
int=123

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllAttrs

func AllAttrs(err error) iter.Seq[slog.Attr]

AllAttrs returns an iter.Seq that iterates over all the attributes of an error tree recursively. The order is from the outermost error to the innermost (see erriter.All). Duplicates are not removed; use GetAttrs to get unique attributes.

func GetAttrs

func GetAttrs(err error) []slog.Attr

GetAttrs returns all the attributes of an error tree, recursively, without duplicates. For each key, only the first attribute is kept (see AllAttrs).

func GetLevel

func GetLevel(err error) (l slog.Level, ok bool)

GetLevel returns the slog.Level associated with an error, if it was wrapped with WrapLevel. The ok boolean indicates whether a level is associated with the error.

func Log

func Log(ctx context.Context, err error, attrs ...slog.Attr)

Log calls LoggerLog with slog.Default.

func LoggerLog

func LoggerLog(ctx context.Context, logger *slog.Logger, err error, attrs ...slog.Attr)

LoggerLog logs an error with a logger. It does nothing if err is nil. It uses the slog.Default logger if logger is nil. It logs at the level slog.LevelError, with err.Error() as the message, and the error tree attributes (GetAttrs) as additional attributes.

func WrapAttrs

func WrapAttrs(err error, attrs ...slog.Attr) error

WrapAttrs wraps an error with attributes (slog.Attr). It returns nil if err is nil. If attrs is empty, it returns err. The error keeps a reference to the attrs slice and does not copy it; the caller must not reuse or modify it after the call. Use AllAttrs or GetAttrs to retrieve the attributes.

func WrapLevel

func WrapLevel(err error, l slog.Level) error

WrapLevel wraps an error with a slog.Level (see GetLevel). It returns nil if err is nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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