cockroachdb / errors

Go error library with error portability over the network
Apache License 2.0
2.09k stars 67 forks source link

Implement slog.LogValuer #107

Open samber opened 1 year ago

samber commented 1 year ago

The new slog library provides a slog.LogValuer interface for formatting attributes.

Documentation here: https://pkg.go.dev/golang.org/x/exp/slog#hdr-Customizing_a_type_s_logging_behavior

I think cockroachdb/errors should implement it.

type customError struct {
    ...
}

func (e customError) Error() string {
    ...
}

// implements slog.LogValuer
func (e customError) LogValue() slog.Value {
    return slog.GroupValue(
            slog.Int("code", e.code),
            slog.String("message", e.msg),
            slog.String("stacktrace", e.stacktrace),
        )
}

slog will be released in go 1.21. But is already available and golang.org/x/exp/slog

nozo-moto commented 1 year ago

@knz @dhartunian Hi maintainers! I've recently implemented the slog.LogValuer feature in our project. I'd appreciate your input on the code quality, documentation, and any potential improvements. Please feel free to comment whenever you have time. Thank you! https://github.com/cockroachdb/errors/pull/129