Skip to content

feat: implement slog.LogValuer on stacktrace error type (#16)#22

Open
sridhar-3009 wants to merge 1 commit into
palantir:masterfrom
sridhar-3009:feat/slog-logvaluer-16
Open

feat: implement slog.LogValuer on stacktrace error type (#16)#22
sridhar-3009 wants to merge 1 commit into
palantir:masterfrom
sridhar-3009:feat/slog-logvaluer-16

Conversation

@sridhar-3009

Copy link
Copy Markdown

Problem

Stacktrace errors currently appear as a flat, multi-line string in log/slog output. Since Go 1.21 introduced log/slog and the slog.LogValuer interface, types can control how they are represented in structured logs. Implementing LogValue() on the *stacktrace type allows callers to get a properly structured log entry instead of a raw Error() dump.

Closes #16.

Changes

slog.go — new file

Implements slog.LogValuer on *stacktrace:

func (st *stacktrace) LogValue() slog.Value {
    return slog.GroupValue(
        slog.String("message", st.Error()),
        slog.Any("frames", collectFrames(st)),
    )
}

The LogValue() method returns a slog.GroupValue with two keys:

Key Value
message Full human-readable error string (same as err.Error())
frames Slice of per-frame groups, each with function, file:line, and the frame-local message when present

slog_test.go — new file

Three new tests:

  • TestLogValue — asserts the interface is implemented and the group contains message and frames keys
  • TestLogValuePropagated — asserts propagated errors carry both outer and inner messages
  • TestLogValueInSlogHandler — logs through a slog.JSONHandler and asserts structured keys appear in the JSON output

go.mod / go.sum — new files

The repository previously had no go.mod. Adding one is required to import log/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)
  • All other previously-passing tests still pass
  • go build ./... succeeds with no warnings

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.
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

Successfully merging this pull request may close these issues.

Implement slog.LogValuer

1 participant