Launch this thingy 😅

This commit is contained in:
Ryan Buckley 2015-05-05 20:15:17 -07:00
commit a1191bfc15
8 changed files with 186 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log

4
Gemfile Normal file
View File

@ -0,0 +1,4 @@
source 'https://rubygems.org'
# Specify your gem's dependencies in grape-middleware-logger.gemspec
gemspec

22
LICENSE.txt Normal file
View File

@ -0,0 +1,22 @@
Copyright (c) 2015 Ryan Buckley
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

46
README.md Normal file
View File

@ -0,0 +1,46 @@
# Grape::Middleware::Logger
Dead until brought to life with the release of Grape v0.12.0
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'grape-middleware-logger'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install grape-middleware-logger
## Usage
class API < Grape::API
use Grape::Middleware::Logger
end
Using Grape with Rails? Add consistent logging and param filtering with
use Grape::Middleware::Logger, {
logger: Rails.logger,
filter: ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
}
## 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

2
Rakefile Normal file
View File

@ -0,0 +1,2 @@
require "bundler/gem_tasks"

View File

@ -0,0 +1,25 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'grape/middleware/logger/version'
Gem::Specification.new do |spec|
spec.name = "grape-middleware-logger"
spec.version = Grape::Middleware::Logger::VERSION
spec.authors = ["Ryan Buckley"]
spec.email = ["arebuckley@gmail.com"]
spec.summary = %q{Logging middleware for Grape apps}
spec.description = %q{Logging middleware for Grape apps, similar to what Rails offers}
spec.homepage = ""
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_dependency 'grape'
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end

View File

@ -0,0 +1,66 @@
require 'grape/middleware/logger/version'
require 'logger'
module Grape
module Middleware
class Logger < Grape::Middleware::Globals
#
# Overrides
#
def before
@start_time = Time.now
super
logger.info ''
logger.info %Q(Started #{env['grape.request'].request_method} "#{env['grape.request'].path}")
logger.info %Q( Parameters: #{parameters})
end
def after(status)
logger.info "Completed #{status} in #{((Time.now - @start_time) * 1000).round(2)}ms"
logger.info ''
end
def call!(env)
@env = env
before
error = catch(:error) { @app_response = @app.call(@env); nil }
if error
after_failure(error)
throw(:error, error)
else
after(@app_response.first)
end
@app_response
end
#
# Helpers
#
def after_failure(error)
logger.info %Q( Error: #{error[:message]}) if error[:message]
after(error[:status])
end
def parameters
request_params = env['grape.request.params'].to_hash
request_params.merge!(env['action_dispatch.request.request_parameters'] || {})
filter_params(request_params)
end
def filter_params(params)
if @options[:filter]
@options[:filter].filter(params)
else
params
end
end
def logger
@logger ||= @options[:logger] || Logger.new(STDOUT)
end
end
end
end

View File

@ -0,0 +1,7 @@
module Grape
module Middleware
class Logger
VERSION = '0.0.1'
end
end
end