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 ¶
- func AllAttrs(err error) iter.Seq[slog.Attr]
- func GetAttrs(err error) []slog.Attr
- func GetLevel(err error) (l slog.Level, ok bool)
- func Log(ctx context.Context, err error, attrs ...slog.Attr)
- func LoggerLog(ctx context.Context, logger *slog.Logger, err error, attrs ...slog.Attr)
- func WrapAttrs(err error, attrs ...slog.Attr) error
- func WrapLevel(err error, l slog.Level) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllAttrs ¶
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 ¶
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 ¶
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 ¶
Log calls LoggerLog with slog.Default.
func LoggerLog ¶
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 ¶
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.
Types ¶
This section is empty.