Skip to content

utgwkk/slogerr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slogerr

Go Reference

slogerr provides Error and NamedError helpers for Go's log/slog package, inspired by the equivalent functions in uber-go/zap.

Each helper returns a slog.Attr that encodes structured error information:

  • msg — the error message from err.Error()
  • verbose — the %+v representation, added only when it differs from msg (e.g. errors carrying a stack trace)
  • causes — the individual errors when err wraps multiple errors via errors.Join or fmt.Errorf("%w %w", ...)

Installation

go get github.com/utgwkk/slogerr

Usage

import (
    "errors"
    "log/slog"

    "github.com/utgwkk/slogerr"
)

// Basic usage — key is "error"
slog.Info("request failed", slogerr.Error(err))

// Custom key
slog.Info("request failed", slogerr.NamedError("cause", err))

// nil is a no-op: returns a zero-value slog.Attr that handlers skip
slog.Info("maybe failed", slogerr.Error(nil))

// errors.Join: individual causes are recorded under "causes"
joined := errors.Join(errors.New("db error"), errors.New("timeout"))
slog.Info("multiple errors", slogerr.Error(joined))

Output format

The attribute value is a slog.GroupValue, so with slog.NewJSONHandler the output looks like:

Simple error

{"msg":"request failed","error":{"msg":"something went wrong"}}

Error with verbose representation (e.g. from github.com/pkg/errors)

{"msg":"request failed","error":{"msg":"something went wrong","verbose":"something went wrong\nmain.main()\n\t/app/main.go:42"}}

Joined errors

{"msg":"request failed","error":{"msg":"db error\ntimeout","causes":{"0":{"msg":"db error"},"1":{"msg":"timeout"}}}}

Note: slog's type system has no native array kind; causes are represented as a group with numeric string keys ("0", "1", ...) rather than a JSON array.

Acknowledgments

This library is inspired by the Error and NamedError functions in uber-go/zap (MIT License, Copyright (c) 2016–2024 Uber Technologies, Inc.).

About

log/slog attributes for errors, inspired by uber-go/zap

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages