diff --git a/lib/lograge/rails_ext/action_dispatch/debug_exceptions.rb b/lib/lograge/rails_ext/action_dispatch/debug_exceptions.rb index 1b9e32e9..c5cea30a 100644 --- a/lib/lograge/rails_ext/action_dispatch/debug_exceptions.rb +++ b/lib/lograge/rails_ext/action_dispatch/debug_exceptions.rb @@ -1,4 +1,8 @@ -require 'action_dispatch/middleware/debug_exceptions' +require 'action_dispatch' + +# Require needed to workaround bug: +# https://github.com/rails/rails/issues/33634 +require 'active_support/core_ext/hash/indifferent_access' module Lograge module DebugExceptions diff --git a/spec/lograge_spec.rb b/spec/lograge_spec.rb index ce3afeb5..0733bd53 100644 --- a/spec/lograge_spec.rb +++ b/spec/lograge_spec.rb @@ -212,4 +212,57 @@ def current_user_id end end end + + describe 'handling exceptions' do + let(:app_config) do + double(config: + ActiveSupport::OrderedOptions.new.tap do |config| + config.action_dispatch = double(rack_cache: false) + config.lograge = ActiveSupport::OrderedOptions.new + end) + end + let(:debug_exceptions) do + ActionDispatch::DebugExceptions.new(->(_) { raise }) + end + let(:output) { StringIO.new } + let(:logger) { Logger.new(output) } + let(:env) do + Rack::MockRequest.env_for( + '', + 'action_dispatch.show_detailed_exceptions' => true, + 'action_dispatch.logger' => logger + ) + end + + before do + Lograge.setup(app_config) + Lograge.logger = logger + end + + it 'adds formatted exception log' do + debug_exceptions.call(env) + expect(output.string).to match(/status=500 error='RuntimeError: '/) + end + + it 'removes original exception log' do + debug_exceptions.call(env) + expect(output.string).not_to match(/FATAL -- : RuntimeError/) + end + + context 'when keep_original_rails_log is true' do + before do + app_config.config.lograge.keep_original_rails_log = true + end + + it 'adds formatted exception log' do + debug_exceptions.call(env) + expect(output.string).to match(/status=500 error='RuntimeError: '/) + end + + it 'keeps original exception log' do + debug_exceptions.call(env) + expect(output.string).to match(/FATAL -- : RuntimeError/) + end + end + end end