Take the console location from the record (closes #36) #41

Merged
clawbot merged 1 commits from issue-36-console-location into next 2026-10-06 14:34:48 +02:00
Collaborator

Fixes #36.

ConsoleHandler used to find the file and line it prints by walking a fixed number of stack frames up from Handle. That count only fit a record logged through a slog.Logger and delivered through MultiplexHandler. It now reads them from the record's PC, the call site log/slog stores in every record it builds, the way the stdlib text handler does. A record whose PC is zero prints ???:0, as before. callerSkipFrames is gone.

New tests in console_handler_internal_test.go check the printed location against the line of the log call in three cases: a MultiplexHandler holding a ConsoleHandler, a ConsoleHandler used with slog.New, and a record built with runtime.Callers and passed to Handle. A fourth checks the ???:0 placeholder. Each test writes its log call inside a one-line function passed to lineOf, which returns the line it was called from; moving the log call to another line breaks the test.

The README's "When delivery fails" section no longer warns about the wrong location; it says the location comes from the record's PC, set with runtime.Callers.

Judgement call: the default-handler test builds a MultiplexHandler holding a ConsoleHandler instead of using slog.Default(), because the installed default holds a JSONHandler whenever stdout is not a terminal, which is the case under test.

Judgement call: the README gains one sentence on setting PC, so a reader following its advice does not get ???:0.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/simplelog/issues/36. `ConsoleHandler` used to find the file and line it prints by walking a fixed number of stack frames up from `Handle`. That count only fit a record logged through a `slog.Logger` and delivered through `MultiplexHandler`. It now reads them from the record's `PC`, the call site `log/slog` stores in every record it builds, the way the stdlib text handler does. A record whose `PC` is zero prints `???:0`, as before. `callerSkipFrames` is gone. New tests in `console_handler_internal_test.go` check the printed location against the line of the log call in three cases: a `MultiplexHandler` holding a `ConsoleHandler`, a `ConsoleHandler` used with `slog.New`, and a record built with `runtime.Callers` and passed to `Handle`. A fourth checks the `???:0` placeholder. Each test writes its log call inside a one-line function passed to `lineOf`, which returns the line it was called from; moving the log call to another line breaks the test. The README's "When delivery fails" section no longer warns about the wrong location; it says the location comes from the record's `PC`, set with `runtime.Callers`. Judgement call: the default-handler test builds a `MultiplexHandler` holding a `ConsoleHandler` instead of using `slog.Default()`, because the installed default holds a `JSONHandler` whenever stdout is not a terminal, which is the case under test. Judgement call: the README gains one sentence on setting `PC`, so a reader following its advice does not get `???:0`. Model: opus-5-5
clawbot added the needs-review label 2026-10-06 14:04:12 +02:00
clawbot self-assigned this 2026-10-06 14:04:12 +02:00
clawbot added 1 commit 2026-10-06 14:04:12 +02:00
Take the console location from the record (closes #36)
check / check (push) Successful in 30s
check / check (pull_request) Successful in 22s
9710d3f05f
ConsoleHandler found the file and line it prints by walking a fixed
number of stack frames up from itself, which only fit a record logged
through a slog.Logger and delivered through MultiplexHandler. It now
reads them from the record's PC, the call site slog stores, so a
ConsoleHandler used with slog.New and a record passed to Handle
directly print the right location too. A record whose PC is zero
prints ???:0 as before. The README drops its warning about the wrong
location and says how to set PC instead.

Model: opus-5-5
Author
Collaborator

Review passed.

Model: opus-5-5

Review passed. Model: opus-5-5
clawbot merged commit 16fc20b81c into next 2026-10-06 14:34:48 +02:00
clawbot deleted branch issue-36-console-location 2026-10-06 14:34:48 +02:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/simplelog#41