grape_rails_logger
Rails-compatible structured logging for Grape APIs with ActiveRecord timing, parameter filtering, and exception tracking.
Installation
Add to your Gemfile:
gem "grape_rails_logger"Run bundle install or gem install grape_rails_logger.
Usage
In a Rails app, adding the gem is enough. The Railtie patches Grape::Endpoint#build_stack, subscribes to grape.request, and writes structured data through Rails.logger. Any Rails logger works; pair with activesupport-json_logging for JSON lines.
What gets logged
Each request logs structured data with:
- Request metadata:
method,path,status,duration,host,remote_addr,request_id - Route information:
controller,action,source_location(file:line) - Performance metrics:
duration(ms),db(ActiveRecord query time in ms),db_calls(SQL query count) - Parameters:
params(filtered with Railsfilter_parameters) - Exceptions:
exceptionobject withclass,message, andbacktrace(non-production only)
Configuration
Configure parameter filtering in config/initializers/filter_parameter_logging.rb:
Rails.application.config.filter_parameters += [
:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]When Rails ParameterFilter is not available, the gem falls back to manual key filtering with patterns aligned to those defaults (passw, secret, token, _key, crypt, salt, certificate, otp, ssn, authorization).
Debug tracing
Optional DebugTracer middleware traces requests when the debug gem is present and TRACE is set:
class API < Grape::API
use GrapeRailsLogger::DebugTracer
endEnable tracing: TRACE=1 rails server. Without the debug gem, the middleware does not start a tracer.
Compatibility
- Rails 7.0, 7.1, 7.2, 8.0+
- Grape >= 1.6, < 5.0
- Ruby >= 3.4
In Rails 7.1+, request state uses ActiveSupport::IsolatedExecutionState. On Rails 7.0, it uses Thread.current.
Development
bundle install
bundle exec appraisal generate
bundle exec polyrun parallel-rspec --workers 5 --merge-failures
bin/appraisals
bundle exec standardrb --fixContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/amkisko/grape_rails_logger.rb
Contribution policy:
- New features are not necessarily added to the gem
- Pull request should have test coverage for affected parts
- Pull request should have changelog entry
Review policy:
- It might take up to 2 calendar weeks to review and merge critical fixes
- It might take up to 6 calendar months to review and merge pull request
- It might take up to 1 calendar year to review an issue
Publishing
rm grape_rails_logger-*.gem
gem build grape_rails_logger.gemspec
gem push grape_rails_logger-*.gemOr use the release script:
usr/bin/release.rbLinks
- GitHub
- GitLab
- RubyGems
-
Versions Atom (feed id
9849778222370) -
Versions Atom (grape-rails-logger) (feed id
9842277577683) - libraries.io
- Deps.dev
- SonarCloud
- Snyk
- Codecov
- OpenSSF Scorecard
License
The gem is available as open source under the terms of the MIT License.
Sponsors
Sponsored by Kisko Labs.