Hacker News new | ask | show | jobs
by sveinnthorarins 278 days ago
Cool article.

I really like structured logs and am pleased the Go team saw the benefits of bringing it into the standard library.

However, I feel like errors should be able to hold slog attributes. It makes for some very useful and easy error logging, especially when the logging takes place far up the execution chain from where the error happened.

This is easily possible with a custom error type and some log functions. I have published on GitHub my small and crude implementation that I use in a few hobby projects, MIT licensed, if anyone is interested. https://github.com/sveinnthorarins/sterlo

1 comments

I felt the same way, and made my own package for adding slog attributes to an error for easier logging. It's usually all I need for my own custom errors.

https://github.com/Danlock/pkg/blob/main/errors/attr_test.go

I can understand why it's not in the stdlib though, it seems easy enough to run into key overwriting issues if a dependency returned custom errors with attributes.

I appreciate the built in support slog has for slog.GroupValue and the slog.LogValuer interface that enables everyone to build a solution best for their needs.