Grape::Middleware::Logger
Simple logger for Grape apps. Logs request path, parameters, status and time taken. Also logs exceptions and error responses (thrown by error!
).
Installation
Add this line to your application's Gemfile:
gem 'grape', '>= 0.12.0'
gem 'grape-middleware-logger'
Usage
class API < Grape::API
use Grape::Middleware::Logger
end
Rails
Using Grape with Rails? Rails.logger
will be used by default.
Custom setup
Want to customize the logging? You can provide a logger
option.
Example using a CustomLogger and parameter sanitization:
use Grape::Middleware::Logger, {
logger: CustomLogger.new,
filter: ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
}
The logger
option can be any object that responds to .info(msg)
The filter
option can be any object that responds to .filter(params_hash)
Example output
Get
Started GET "/v1/reports/101"
Parameters: {"id"=>"101"}
Completed 200 in 6.29ms
Error
Started GET "/v1/reports/101"
Parameters: {"id"=>"101"}
Error: {:error=>"undefined something something bad", :detail=>"Whoops"}
Completed 422 in 6.29ms
Credits
Big thanks to jadent's question/answer on stackoverflow for easily logging error responses. Borrowed some motivation from the grape_logging gem and would love to see these two consolidated at some point.
Contributing
- Fork it ( https://github.com/ridiculous/grape-middleware-logger/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request