Expose API logger via Grape::Endpoint#logger#2704
Merged
Merged
Conversation
dc82494 to
7795f93
Compare
Danger ReportNo issues found. |
9136c68 to
08376eb
Compare
3 tasks
dblock
approved these changes
May 8, 2026
Add a `Grape::Endpoint#logger` accessor that delegates to `options[:for].logger`. The API's configured logger is now reachable without a helper from inside any context whose `self` is the endpoint: route handlers, `before` / `before_validation` / `after_validation` / `after` / `finally` filters, and `rescue_from` blocks. A user-defined `helpers do; def logger; ...; end` still wins, since helpers are mixed into the endpoint's singleton class — so the addition is backwards compatible. Drops the helper workaround from `benchmark/remounting.rb` to illustrate the simplification. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
08376eb to
8ee2518
Compare
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.
Adds a
Grape::Endpoint#loggeraccessor so the API's configured logger is reachable inside any endpoint context — without writing a helper.Before
Calling
loggerinside a route, filter, orrescue_fromblock raisedNoMethodError. The documented workaround (and the one used inbenchmark/remounting.rb) was:After
loggerworks in every context whoseselfis the endpoint:before/before_validation/after_validation/after/finallyfiltersrescue_fromblocksBackwards compatibility
A user-defined
helpers do; def logger; …; endstill wins. Helpers are mixed into the endpoint's singleton class (call!doessingleton_class.include(@helpers)), and singleton-class methods take precedence over instance methods on the class. So existing code that overrodeloggercontinues to override it.A spec verifies this:
helpers do; def logger; 'overridden-helper'; end; endcausesloggerto return the helper's value, not the API's configured logger.Files
lib/grape/endpoint.rbdef logger; options[:for].logger; end.benchmark/remounting.rbhelpers do; def logger; ... endworkaround.spec/grape/endpoint/logger_spec.rbrescue_from, and the helpers-override-wins case.CHANGELOG.mdTest plan
bundle exec rspec spec/grape/endpoint/logger_spec.rb— 4 examples, 0 failuresbundle exec rspec— full suite green (2,258 examples, up from 2,254)bundle exec rubocop lib/ spec/— cleanWhy this is its own PR
Splits cleanly out of #2703 (which catches exceptions inside
rescue_fromblocks). That PR's example wanted to calllogger.error(...)from inside the user's handler — which doesn't work today, hence this addition. They're independent: #2703 ships the rescue redispatch; this ships the logger accessor.🤖 Generated with Claude Code