Skip to content

Commit 2738115

Browse files
author
Marc Grimme
committed
Overwrite middleware to handle errors.
1 parent 8e7d5df commit 2738115

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module LogStasher
2+
module ActionDispatch
3+
class DebugExceptions < ::ActionDispatch::DebugExceptions
4+
private
5+
def log_error(env, wrapper)
6+
logger = logger(env)
7+
return unless logger
8+
9+
::ActiveSupport::Deprecation.silence do
10+
logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n"
11+
end
12+
end
13+
14+
def build_exception_hash(wrapper)
15+
exception = wrapper.exception
16+
trace = wrapper.application_trace
17+
trace = wrapper.framework_trace if trace.empty?
18+
19+
{ error:
20+
({ exception: exception.class, message: exception.message, trace: trace}.
21+
merge!( exception.respond_to?(:annotated_source_code) && { annotated_source_code: exception.annoted_source_code } || {} ))
22+
}
23+
end
24+
end
25+
end
26+
end

lib/logstasher/railtie.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rails/railtie'
22
require 'action_view/log_subscriber'
33
require 'action_controller/log_subscriber'
4+
require 'logstasher/rails_ext/rack/debug_exceptions'
45
require 'socket'
56

67
module LogStasher
@@ -33,8 +34,10 @@ class Railtie < Rails::Railtie
3334
LogStasher.setup_before(app.config.logstasher) if app.config.logstasher.enabled
3435
end
3536

36-
initializer :logstasher do
37-
config.after_initialize do
37+
initializer :logstasher do |app|
38+
app.config.middleware.use ::LogStasher::ActionDispatch::DebugExceptions
39+
app.config.middleware.delete ::ActionDispatch::DebugExceptions if Rails.env.production?
40+
app.config.after_initialize do
3841
LogStasher.setup(config.logstasher) if config.logstasher.enabled
3942
end
4043
end

0 commit comments

Comments
 (0)