Skip to content

fix(logger): pass the caller pc to handler wrappers - #53

Merged
josuegiron merged 1 commit into
masterfrom
fix/wrapper-caller-pc
Nov 21, 2026
Merged

josuegiron merged 1 commit into
masterfrom
fix/wrapper-caller-pc

Conversation

@josuegiron

Copy link
Copy Markdown
Member

What

A handler wrapper set with WithHandlerWrapper / SetHandlerWrapper received a slog.Record whose PC pointed at logger.(*Logger).log (logger/logger.go), because the logger called slog.Logger.Log. A wrapper using AddSource or a bridge that reads record.PC (otelslog-style) reported the wrong call site. The JSON source attribute was already right (LOG-06); the wrapper path (LOG-07) never met it.

Found by the epic-assessment retrospective (finding 6).

How

  • (*Logger).log builds the slog.Record itself with the caller's PC, taken from the same program counters as the source attribute (calldepth honored, SetCalldepth included), and calls Handler().Handle. This is the pattern the log/slog docs give for wrapper functions.
  • With source metadata off, the PC comes from callerPC, which does not allocate (the same work slog.Logger.Log did before).
  • A nil context becomes context.Background(), as slog.Logger.Log did.
  • README "Handler wrappers" states what record.PC holds since v1.7.5.

Constitution VIII deviation (justified here per Governance): the handler's error is discarded with _ =. A logging call has no error result and no other channel to report a failed write; slog.Logger.Log dropped the same error before this change, so behavior is unchanged. The comment at the call states this.

Tests

  • TestHandlerWrapperSeesCallerPC (instance API, instance with source off, package-level helpers): failed on master with record.PC = {function:log file:logger.go line:216}, passes now.
  • TestHandlerWrapperNeverSeesNilContext: fails when the nil-context guard is removed.

Local verification

  • gofmt -l . empty (root, otel)
  • go vet ./... clean (root, otel)
  • go test -race ./... pass (root, otel)
  • govulncheck ./... no vulnerabilities (root, otel)
  • go test -bench .: identical B/op and allocs/op; ns/op equal or lower

PATCH: release as log v1.7.5.

🤖 Generated with Claude Code

A wrapper set with WithHandlerWrapper or SetHandlerWrapper saw
record.PC point at logger.(*Logger).log. Build the record with the
caller's PC, the same call site as the source attribute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@josuegiron
josuegiron merged commit bc7a56c into master Nov 21, 2026
2 checks passed
@josuegiron
josuegiron deleted the fix/wrapper-caller-pc branch November 21, 2026 19:52
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.

1 participant