Skip to content

Expose API logger via Grape::Endpoint#logger#2704

Merged
ericproulx merged 1 commit into
masterfrom
feature/endpoint-logger
May 9, 2026
Merged

Expose API logger via Grape::Endpoint#logger#2704
ericproulx merged 1 commit into
masterfrom
feature/endpoint-logger

Conversation

@ericproulx
Copy link
Copy Markdown
Contributor

Adds a Grape::Endpoint#logger accessor so the API's configured logger is reachable inside any endpoint context — without writing a helper.

Before

Calling logger inside a route, filter, or rescue_from block raised NoMethodError. The documented workaround (and the one used in benchmark/remounting.rb) was:

class VotingApi < Grape::API
  logger Logger.new($stdout)

  helpers do
    def logger
      VotingApi.logger
    end
  end

  get('/votes') { logger.info('hit') }
end

After

class VotingApi < Grape::API
  logger Logger.new($stdout)
  get('/votes') { logger.info('hit') }
end

logger works in every context whose self is the endpoint:

  • route handlers
  • before / before_validation / after_validation / after / finally filters
  • rescue_from blocks
  • helpers

Backwards compatibility

A user-defined helpers do; def logger; …; end still wins. Helpers are mixed into the endpoint's singleton class (call! does singleton_class.include(@helpers)), and singleton-class methods take precedence over instance methods on the class. So existing code that overrode logger continues to override it.

A spec verifies this: helpers do; def logger; 'overridden-helper'; end; end causes logger to return the helper's value, not the API's configured logger.

Files

File What
lib/grape/endpoint.rb One-line def logger; options[:for].logger; end.
benchmark/remounting.rb Removes the now-unnecessary helpers do; def logger; ... end workaround.
spec/grape/endpoint/logger_spec.rb 4 examples — route, all five filter types, rescue_from, and the helpers-override-wins case.
CHANGELOG.md Features entry.

Test plan

  • bundle exec rspec spec/grape/endpoint/logger_spec.rb — 4 examples, 0 failures
  • bundle exec rspec — full suite green (2,258 examples, up from 2,254)
  • bundle exec rubocop lib/ spec/ — clean

Why this is its own PR

Splits cleanly out of #2703 (which catches exceptions inside rescue_from blocks). That PR's example wanted to call logger.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

@ericproulx ericproulx force-pushed the feature/endpoint-logger branch from dc82494 to 7795f93 Compare May 8, 2026 17:59
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 8, 2026

Danger Report

No issues found.

View run

@ericproulx ericproulx force-pushed the feature/endpoint-logger branch 2 times, most recently from 9136c68 to 08376eb Compare May 8, 2026 18:04
@ericproulx ericproulx requested a review from dblock May 8, 2026 18:44
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>
@ericproulx ericproulx force-pushed the feature/endpoint-logger branch from 08376eb to 8ee2518 Compare May 9, 2026 17:47
@ericproulx ericproulx merged commit 037476e into master May 9, 2026
79 checks passed
@ericproulx ericproulx deleted the feature/endpoint-logger branch May 9, 2026 18:37
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.

2 participants