https://github.com/amkisko/grape_rails_logger.rb
Unified, Rails-compatible JSON request logging for Grape APIs with ActiveRecord timing, parameter filtering, and structured context.
https://github.com/amkisko/grape_rails_logger.rb
activerecord grape logging monitoring rails security structured-logging
Last synced: 2 months ago
JSON representation
Unified, Rails-compatible JSON request logging for Grape APIs with ActiveRecord timing, parameter filtering, and structured context.
- Host: GitHub
- URL: https://github.com/amkisko/grape_rails_logger.rb
- Owner: amkisko
- License: mit
- Created: 2025-11-04T14:11:44.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2026-02-10T15:08:21.000Z (4 months ago)
- Last Synced: 2026-02-10T19:52:50.042Z (4 months ago)
- Topics: activerecord, grape, logging, monitoring, rails, security, structured-logging
- Language: Ruby
- Homepage: https://rubygems.org/gems/grape_rails_logger
- Size: 175 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
- Governance: GOVERNANCE.md
Awesome Lists containing this project
README
# grape_rails_logger
[](https://badge.fury.io/rb/grape_rails_logger) [](https://github.com/amkisko/grape_rails_logger.rb/actions/workflows/test.yml) [](https://codecov.io/gh/amkisko/grape_rails_logger.rb)
Rails-compatible structured logging for Grape APIs with ActiveRecord timing, parameter filtering, and exception tracking.
Sponsored by [Kisko Labs](https://www.kiskolabs.com).
## Installation
Add to your Gemfile:
```ruby
gem "grape_rails_logger"
```
Run `bundle install` or `gem install grape_rails_logger`.
## Usage
The gem works automatically in Rails applications. No configuration needed. It automatically patches `Grape::Endpoint#build_stack` to instrument requests and subscribes to `grape.request` notifications, logging structured data via `Rails.logger`. Works with any Rails logger or integrates with `activesupport-json_logging` for JSON output.
## 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` (automatically filtered using Rails `filter_parameters`)
- Exceptions: `exception` object with `class`, `message`, and `backtrace` (non-production only)
## Configuration
Configure parameter filtering in `config/initializers/filter_parameter_logging.rb`:
```ruby
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 filtering that detects sensitive patterns (password, secret, token, key) in parameter keys.
## Debug tracing
Optional `DebugTracer` middleware provides detailed request tracing when the `debug` gem is installed and `TRACE` environment variable is set:
```ruby
class API < Grape::API
use GrapeRailsLogger::DebugTracer
end
```
Enable tracing: `TRACE=1 rails server`. The middleware gracefully degrades if the `debug` gem is not installed.
## Compatibility
- Rails 6.0, 6.1, 7.0, 7.1, 7.2, 8.0+
- Grape >= 1.6
- Ruby >= 2.7
In Rails 7.1+, the gem uses `ActiveSupport::IsolatedExecutionState` for improved thread/Fiber safety. In Rails 6-7.0, it falls back to `Thread.current`.
## Development
```bash
bundle install
bundle exec appraisal generate
bundle exec rspec
bin/appraisals
bundle exec standardrb --fix
```
## Contributing
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
```sh
rm grape_rails_logger-*.gem
gem build grape_rails_logger.gemspec
gem push grape_rails_logger-*.gem
```
Or use the release script:
```sh
usr/bin/release.rb
```
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).