Go to file
sirnicolaz a4daa56c53 add tests for array response behaviour 2015-11-18 11:56:52 +01:00
lib/grape/middleware add tests for array response behaviour 2015-11-18 11:56:52 +01:00
spec add tests for array response behaviour 2015-11-18 11:56:52 +01:00
.gitignore add .ruby-version to .gitignore 2015-05-09 14:20:21 -07:00
Gemfile Launch this thingy 😅 2015-05-05 20:20:23 -07:00
LICENSE.txt Launch this thingy 😅 2015-05-05 20:20:23 -07:00
README.md update readme with Rails infos 2015-10-23 09:48:19 -02:00
Rakefile Specs and update to handle broken state of Grape Globals 2015-05-05 22:22:35 -07:00
grape-middleware-logger.gemspec update to work with Grape 0.12.0 and log exceptions 2015-07-11 12:41:22 -07:00

README.md

Grape::Middleware::Logger

Code Climate Gem Version

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

  1. Fork it ( https://github.com/ridiculous/grape-middleware-logger/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request