Logs:
error!
Add this line to your application's Gemfile:
gem 'grape', '>= 0.17' gem 'grape-middleware-logger'
require 'grape' require 'grape/middleware/logger' class API < Grape::API # @note Make sure this is above your first +mount+ insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger end
Server requests will be logged to STDOUT by default.
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
POST
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
The middleware logger can be customized with the following options:
:logger
option can be any object that responds to .info(String)
:condensed
option configures the log output to be on one line instead of multiple. It accepts true
or false
. The default configuration is false
:filter
option can be any object that responds to .filter(Hash)
and returns a hash.:headers
option can be either :all
or array of strings.
:all
, all request headers will be output.For example:
insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger, { logger: Logger.new(STDERR), condensed: true, filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new, headers: %w(version cache-control) }
Rails.logger
and Rails.application.config.filter_parameters
will be used automatically as the default logger and param filterer, respectively. This behavior can be overridden by passing the :logger
or :filter
option when mounting.
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.swap 'Rails::Rack::Logger', '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
If you're using the rackup
command to run your server in development, pass the -q
flag to silence the default rack logger.
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.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4