grape-middleware-logger/README.md

120 lines
3.9 KiB
Markdown
Raw Permalink Normal View History

2016-08-29 10:59:55 +08:00
# A logger for [Grape](https://github.com/ruby-grape/grape) apps
2015-05-10 05:23:49 +08:00
[![Code Climate](https://codeclimate.com/github/ridiculous/grape-middleware-logger/badges/gpa.svg)](https://codeclimate.com/github/ridiculous/grape-middleware-logger) [![Gem Version](https://badge.fury.io/rb/grape-middleware-logger.svg)](http://badge.fury.io/rb/grape-middleware-logger)
2015-12-09 08:30:07 +08:00
[![Build Status](https://travis-ci.org/ridiculous/grape-middleware-logger.svg)](https://travis-ci.org/ridiculous/grape-middleware-logger)
2015-05-06 11:15:17 +08:00
2015-12-16 11:35:48 +08:00
Logs:
* Request path
* Parameters
* Endpoint class name and handler
* Response status
* Duration of the request
* Exceptions
* Error responses from `error!`
2015-05-06 11:15:17 +08:00
## Installation
Add this line to your application's Gemfile:
```ruby
2016-05-12 19:59:31 +08:00
gem 'grape', '>= 0.17'
2015-05-06 11:15:17 +08:00
gem 'grape-middleware-logger'
```
## Usage
2015-07-02 13:57:04 +08:00
```ruby
require 'grape'
require 'grape/middleware/logger'
2015-07-02 13:57:04 +08:00
class API < Grape::API
2016-09-16 18:58:07 +08:00
# @note Make sure this is above your first +mount+
insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger
2015-07-02 13:57:04 +08:00
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-07-12 03:49:31 +08:00
## Example output
2016-04-09 09:57:27 +08:00
GET
2015-07-12 03:49:31 +08:00
```
2015-12-12 07:44:45 +08:00
Started GET "/v1/reports/101" at 2015-12-11 15:40:51 -0800
Processing by ReportsAPI/reports/:id
2015-07-12 03:49:31 +08:00
Parameters: {"id"=>"101"}
Completed 200 in 6.29ms
```
2016-04-09 09:57:27 +08:00
POST
2015-07-12 03:49:31 +08:00
```
2015-12-12 07:44:45 +08:00
Started POST "/v1/reports" at 2015-12-11 15:42:33 -0800
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
```
2016-04-01 15:01:14 +08:00
## Customization
The middleware logger can be customized with the following options:
* The `:logger` option can be any object that responds to `.info(String)`
2017-06-03 00:50:46 +08:00
* The `:condensed` option configures the log output to be on one line instead of multiple. It accepts `true` or `false`. The default configuration is `false`
2016-04-01 15:01:14 +08:00
* The `:filter` option can be any object that responds to `.filter(Hash)` and returns a hash.
* The `:headers` option can be either `:all` or array of strings.
+ If `:all`, all request headers will be output.
+ If array, output will be filtered by names in the array. (case-insensitive)
2016-04-01 15:01:14 +08:00
2016-05-12 19:57:11 +08:00
For example:
```ruby
insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger, {
2016-05-12 19:57:11 +08:00
logger: Logger.new(STDERR),
2017-06-03 00:50:46 +08:00
condensed: true,
filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new,
headers: %w(version cache-control)
2016-05-12 19:57:11 +08:00
}
```
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. This behavior can be overridden by passing the `:logger` or
`:filter` option when mounting.
2015-12-12 17:04:19 +08:00
You may want to disable Rails logging for API endpoints, so that the logging doesn't double-up. You can achieve this
2015-12-12 17:04:19 +08:00
by switching around some middleware. For example:
```ruby
# config/application.rb
2016-05-20 04:57:22 +08:00
config.middleware.swap 'Rails::Rack::Logger', 'SelectiveLogger'
2015-12-12 17:04:19 +08:00
# 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 )
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