feat: implement slog.LogValuer on stacktrace error type (#16)#22
Open
sridhar-3009 wants to merge 1 commit into
Open
feat: implement slog.LogValuer on stacktrace error type (#16)#22sridhar-3009 wants to merge 1 commit into
sridhar-3009 wants to merge 1 commit into
Conversation
Add LogValue() slog.Value method to the *stacktrace type so that stacktrace errors produce structured log output when used with log/slog (available since Go 1.21). The returned slog.GroupValue contains: - "message": the full human-readable error string (same as Error()) - "frames": a list of per-frame groups with "message", "function", and "file:line" for each frame in the error chain Also add go.mod and go.sum to enable module-aware builds, required for importing log/slog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Stacktrace errors currently appear as a flat, multi-line string in
log/slogoutput. Since Go 1.21 introducedlog/slogand theslog.LogValuerinterface, types can control how they are represented in structured logs. ImplementingLogValue()on the*stacktracetype allows callers to get a properly structured log entry instead of a rawError()dump.Closes #16.
Changes
slog.go— new fileImplements
slog.LogValueron*stacktrace:The
LogValue()method returns aslog.GroupValuewith two keys:messageerr.Error())framesfunction,file:line, and the frame-localmessagewhen presentslog_test.go— new fileThree new tests:
TestLogValue— asserts the interface is implemented and the group containsmessageandframeskeysTestLogValuePropagated— asserts propagated errors carry both outer and inner messagesTestLogValueInSlogHandler— logs through aslog.JSONHandlerand asserts structured keys appear in the JSON outputgo.mod/go.sum— new filesThe repository previously had no
go.mod. Adding one is required to importlog/slog(Go 1.21+) and to enable module-aware builds. The module path matches the existing import path used throughout the code:github.com/palantir/stacktrace.Example structured output
With a JSON handler:
{ "level": "ERROR", "msg": "operation failed", "error": { "message": "outer context\n --- at pkg/foo.go:42 (Fn) ---\nCaused by: inner error\n --- at pkg/bar.go:10 (Bar) ---", "frames": [ {"message": "outer context", "function": "Fn", "file": "pkg/foo.go:42"}, {"message": "inner error", "function": "Bar", "file": "pkg/bar.go:10"} ] } }Test plan
go test -run TestLogValue ./...passes (3 new tests)go build ./...succeeds with no warnings