Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement slog.LogValuer #107

Open
samber opened this issue Apr 16, 2023 · 1 comment
Open

Implement slog.LogValuer #107

samber opened this issue Apr 16, 2023 · 1 comment

Comments

@samber
Copy link

samber commented Apr 16, 2023

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
Copy link

@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!
#129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants