2015-12-16 11:22:59 +08:00
# A logger for Grape apps
2015-05-10 05:23:49 +08:00
[](https://codeclimate.com/github/ridiculous/grape-middleware-logger) [](http://badge.fury.io/rb/grape-middleware-logger)
2015-12-09 08:30:07 +08:00
[](https://travis-ci.org/ridiculous/grape-middleware-logger)
2015-05-06 11:15:17 +08:00
2015-12-16 11:22:59 +08:00
Logs the request path, endpoint name, parameters, response status and duration. Exceptions and `error!` responses are logged as well.
2015-05-06 11:15:17 +08:00
## Installation
Add this line to your application's Gemfile:
```ruby
2015-12-12 12:00:13 +08:00
gem 'grape', '>= 0.14.0'
2015-05-06 11:15:17 +08:00
gem 'grape-middleware-logger'
```
## Usage
2015-07-02 13:57:04 +08:00
```ruby
class API < Grape::API
2015-12-12 11:55:05 +08:00
# @note Make sure this above you're first +mount+
2015-07-02 13:57:04 +08:00
use Grape::Middleware::Logger
end
2015-10-23 19:48:19 +08:00
```
2015-12-09 09:00:30 +08:00
Server requests will be logged to STDOUT by default.
2015-12-12 17:04:19 +08:00
## Custom setup
2015-12-09 09:00:30 +08:00
Customize the logging by passing the `logger` option. Example using a CustomLogger and parameter sanitization:
2015-07-02 13:57:04 +08:00
```ruby
2015-10-23 19:48:19 +08:00
use Grape::Middleware::Logger, {
logger: CustomLogger.new,
2015-12-12 17:04:19 +08:00
filter: CustomFilter.new
2015-07-02 13:57:04 +08:00
}
```
2015-10-23 19:48:19 +08:00
The `logger` option can be any object that responds to `.info(msg)`
2015-07-02 14:02:14 +08:00
2015-07-02 14:03:31 +08:00
The `filter` option can be any object that responds to `.filter(params_hash)`
2015-07-12 03:49:31 +08:00
## Example output
Get
```
2015-12-12 07:44:45 +08:00
Started GET "/v1/reports/101" at 2015-12-11 15:40:51 -0800
2015-12-12 12:03:29 +08:00
Processing by ReportsAPI#reports/:id
2015-07-12 03:49:31 +08:00
Parameters: {"id"=>"101"}
Completed 200 in 6.29ms
```
Error
```
2015-12-12 07:44:45 +08:00
Started POST "/v1/reports" at 2015-12-11 15:42:33 -0800
2015-12-12 12:03:29 +08:00
Processing by ReportsAPI#reports
2015-12-12 07:44:45 +08:00
Parameters: {"name"=>"foo", "password"=>"[FILTERED]"}
2015-07-12 03:49:31 +08:00
Error: {:error=>"undefined something something bad", :detail=>"Whoops"}
Completed 422 in 6.29ms
```
2015-12-12 17:04:19 +08:00
## 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:
```ruby
# 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
```
2015-12-12 12:03:29 +08:00
## Rack
If you're using the `rackup` command to run your server in development, pass the `-q` flag to silence the default rack logger.
2015-05-06 11:15:17 +08:00
## Credits
Big thanks to jadent's question/answer on [stackoverflow ](http://stackoverflow.com/questions/25048163/grape-using-error-and-grapemiddleware-after-callback )
for easily logging error responses. Borrowed some motivation from the [grape_logging ](https://github.com/aserafin/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 )
2015-12-15 10:10:37 +08:00
1. Run the test suite with `bin/test`
2015-05-06 11:15:17 +08:00
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