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 #16

Open
samber opened this issue Apr 16, 2023 · 0 comments
Open

Implement slog.LogValuer #16

samber opened this issue Apr 16, 2023 · 0 comments

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 palantir/stacktrace 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

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

1 participant