diff --git a/Gemfile b/Gemfile index 633d308..e3228d9 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,8 @@ source 'https://rubygems.org' gemspec group :test do + gem 'debug' gem 'rake' - gem "factory_girl", "~> 4.0" - gem "rails", "~> 4.2" + gem "factory_girl" #, "~> 4.0" + gem "rails" #, "~> 4.2" end diff --git a/grape-middleware-lograge.gemspec b/grape-middleware-lograge.gemspec index 2b64c97..215c2f4 100644 --- a/grape-middleware-lograge.gemspec +++ b/grape-middleware-lograge.gemspec @@ -2,10 +2,10 @@ Gem::Specification.new do |spec| spec.name = 'grape-middleware-lograge' - spec.version = '1.2.3' + spec.version = '1.3.1' spec.platform = Gem::Platform::RUBY - spec.authors = ['Ryan Buckley', 'Paul Chavard'] - spec.email = ['arebuckley@gmail.com', 'paul+github@chavard.net'] + spec.authors = ['Ryan Buckley', 'Paul Chavard','Josh Buermann'] + spec.email = ['arebuckley@gmail.com', 'paul+github@chavard.net','buermann@gmail.com'] spec.summary = %q{A logger for the Grape framework} spec.description = %q{Logging middleware for the Grape framework, that uses Lograge} spec.homepage = 'https://github.com/tchak/grape-middleware-lograge' @@ -16,10 +16,10 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] - spec.add_dependency 'grape', '>= 0.14', '< 1' + spec.add_dependency 'grape', '>= 0.14' spec.add_dependency 'lograge', '~> 0.3' - spec.add_development_dependency 'bundler', '~> 1.7' - spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rspec', '>= 3.2', '< 4' + spec.add_development_dependency 'bundler' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rspec' end diff --git a/lib/grape/middleware/lograge.rb b/lib/grape/middleware/lograge.rb index 1dc4f03..5330046 100644 --- a/lib/grape/middleware/lograge.rb +++ b/lib/grape/middleware/lograge.rb @@ -21,11 +21,10 @@ def initialize(_, options = {}) def before super - @db_duration = 0 + @db_runtime = 0 - @db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - @db_duration += event.duration + @db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, start, ending, _transaction_id, _payload| + @db_runtime += 1000.0 * (ending - start) if ending && start end if defined?(ActiveRecord) ActiveSupport::Notifications.instrument("start_processing.grape", raw_payload) @@ -58,15 +57,27 @@ def call!(env) after(payload, response.status) end + # Parse request parameters from the PUT/POST body and include them in the params payload + body = env[Grape::Env::API_REQUEST_INPUT] + if body.present? + fmt = request.media_type ? mime_types[request.media_type] : options[:default_format] + parser = Grape::Parser.parser_for fmt + if content_type_for(fmt) + body_params = parser.call(body, env) + payload[:params].merge!(body_params) + end + end + @app_response end end def after(payload, status) + ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription payload[:status] = status payload[:format] = env['api.format'] payload[:version] = env['api.version'] - payload[:db_runtime] = @db_duration + payload[:db_runtime] = @db_runtime end def after_exception(payload, e) @@ -90,8 +101,9 @@ def after_failure(payload, error) end def parameters - request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS].to_hash + request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS] #Grape::Env::GRAPE_REQUEST].params request_params.merge!(env['action_dispatch.request.request_parameters'.freeze] || {}) # for Rails + if @options[:filter] @options[:filter].filter(request_params) else @@ -113,6 +125,10 @@ def raw_payload } end + def request + env[Grape::Env::GRAPE_REQUEST] + end + def endpoint env[Grape::Env::API_ENDPOINT] end diff --git a/lib/grape/middleware/lograge/railtie.rb b/lib/grape/middleware/lograge/railtie.rb index 511af3c..b475cac 100644 --- a/lib/grape/middleware/lograge/railtie.rb +++ b/lib/grape/middleware/lograge/railtie.rb @@ -1,7 +1,13 @@ class Grape::Middleware::Lograge::Railtie < Rails::Railtie initializer 'grape.middleware.lograge', after: :load_config_initializers do - Grape::Middleware::Lograge.filter = ActionDispatch::Http::ParameterFilter.new Rails.application.config.filter_parameters + filter_class = Rails::VERSION::MAJOR > 5 ? ActiveSupport::ParameterFilter : ActionDispatch::Http::ParameterFilter - ::Lograge::RequestLogSubscriber.attach_to :grape + Grape::Middleware::Lograge.filter = filter_class.new(Rails.application.config.filter_parameters) + + if Gem::Version.new( Lograge::VERSION ) > Gem::Version.new('0.10.9') + Lograge::LogSubscribers::Base.attach_to :grape + else + ::Lograge::RequestLogSubscriber.attach_to :grape + end end end diff --git a/spec/lib/grape/middleware/lograge_spec.rb b/spec/lib/grape/middleware/lograge_spec.rb index 9d347d2..1826e25 100644 --- a/spec/lib/grape/middleware/lograge_spec.rb +++ b/spec/lib/grape/middleware/lograge_spec.rb @@ -53,7 +53,7 @@ expect(app).to receive(:call).with(env).and_return(app_response) allow(subject).to receive(:before) allow(subject).to receive(:after) - expect(subject.call!(env)).to eq app_response + expect(subject.call!(env).to_a).to eq app_response end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dc98187..b4cf62b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'ostruct' +require 'debug' require 'factory_girl' require 'grape/middleware/lograge'