Go to file
Ryan Buckley 1e9a84fb85 add how to run tests to readme 2015-12-14 18:10:37 -08:00
bin cleanup test suites 2015-12-12 00:20:04 -08:00
lib/grape/middleware Refactor logger conditional to use coercion and parallel assignment 2015-12-14 18:10:26 -08:00
spec Refactor logger conditional to use coercion and parallel assignment 2015-12-14 18:10:26 -08:00
.gitignore add .ruby-version to .gitignore 2015-05-09 14:20:21 -07:00
.travis.yml cleanup test suites 2015-12-12 00:20:04 -08:00
Gemfile use railtie 2015-12-12 00:05:32 -08:00
LICENSE.txt Launch this thingy 😅 2015-05-05 20:20:23 -07:00
README.md add how to run tests to readme 2015-12-14 18:10:37 -08:00
Rakefile add travis config 2015-12-08 16:28:51 -08:00
grape-middleware-logger.gemspec version 1.5.0 2015-12-12 01:05:53 -08:00

README.md

Grape::Middleware::Logger

Code Climate Gem Version Build Status

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.14.0'
gem 'grape-middleware-logger'

Usage

class API < Grape::API
  # @note Make sure this above you're first +mount+
  use Grape::Middleware::Logger
end

Server requests will be logged to STDOUT by default.

Custom setup

Customize the logging by passing the logger option. Example using a CustomLogger and parameter sanitization:

use Grape::Middleware::Logger, {
  logger: CustomLogger.new,
  filter: CustomFilter.new
}

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" at 2015-12-11 15:40:51 -0800
Processing by ReportsAPI#reports/:id
  Parameters: {"id"=>"101"}
Completed 200 in 6.29ms

Error

Started POST "/v1/reports" at 2015-12-11 15:42:33 -0800
Processing by ReportsAPI#reports
  Parameters: {"name"=>"foo", "password"=>"[FILTERED]"}
  Error: {:error=>"undefined something something bad", :detail=>"Whoops"}
Completed 422 in 6.29ms

Using Rails?

Rails.logger and Rails.application.config.filter_parameters will be used automatically as the default logger and param filterer, respectively.

You may want to disable Rails logging for API endpoints, so that the logging doesn't double-up. You can achieve this by switching around some middleware. For example:

# config/application.rb
config.middleware.delete 'Rails::Rack::Logger'
config.middleware.insert_after 'ActionDispatch::RequestId', 'SelectiveLogger'

# config/initializers/selective_logger.rb
class SelectiveLogger
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['PATH_INFO'] =~ %r{^/api}
      @app.call(env)
    else
      Rails::Rack::Logger.new(@app).call(env)
    end
  end
end

Rack

If you're using the rackup command to run your server in development, pass the -q flag to silence the default rack logger.

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. Run the test suite with bin/test
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request