Skip to content

Document how to add context to every log line of a request - #68

Merged
PetrHeinz merged 2 commits into
mainfrom
claude/issue-28-context-docs
Oct 2, 2026
Merged

PetrHeinz merged 2 commits into
mainfrom
claude/issue-28-context-docs

Conversation

@PetrHeinz

Copy link
Copy Markdown
Member

The example project's README only showed around_action for adding context to a request's logs, so readers of #28 could not get the user onto the Started GET … and Completed 200 OK … lines: those are logged by the HTTPEvents Rack middleware before the controller runs. The gem has always had the right hook for this, Logtail::Integrations::Rack::UserContext.custom_user_hash, but no README mentioned it.

The "Context" section of example-project/README.md now explains:

  • that Devise (any Warden strategy) and Clearance users are picked up automatically, by a middleware that runs before the request line is logged;
  • custom_user_hash: a lambda that finds the user in the Rack env and returns a hash, logged as context.user on every line of the request, including the request and response lines;
  • a custom middleware wrapping the request in Logtail.with_context for any other per-request context, inserted before Logtail::Integrations::Rack::HTTPEvents from an initializer;
  • around_action stays, as the option when the user is only known inside the controller, with its limit stated.

README.md links to that section.

Both snippets were run as written on the example project (Rails 8.1.4, Ruby 3.3.4, logtail-rails 0.2.14, logtail-rack 0.2.8, logtail 0.1.20) against a local sink: the lambda's hash and the middleware's tenant key arrive on the Started, Processing, in-action, Rendered and Completed rows; the around_action context arrives on the in-action and Rendered rows only.

🤖 Generated with Claude Code

The example project's README only showed around_action, which cannot
reach the request and response lines logged by the HTTPEvents
middleware. Describe UserContext.custom_user_hash and a custom
middleware inserted before HTTPEvents, and state around_action's limit.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@PetrHeinz
PetrHeinz marked this pull request as ready for review October 2, 2026 11:07
@PetrHeinz
PetrHeinz merged commit 6c85935 into main Oct 2, 2026
66 checks passed
@PetrHeinz
PetrHeinz deleted the claude/issue-28-context-docs branch October 2, 2026 16:05
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