Skip to content

Commit

Permalink
Overwrite middleware to handle errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Grimme committed Jun 25, 2016
1 parent 8e7d5df commit 2738115
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/logstasher/rails_ext/rack/debug_exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module LogStasher
module ActionDispatch
class DebugExceptions < ::ActionDispatch::DebugExceptions
private
def log_error(env, wrapper)
logger = logger(env)
return unless logger

::ActiveSupport::Deprecation.silence do
logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n"
end
end

def build_exception_hash(wrapper)
exception = wrapper.exception
trace = wrapper.application_trace
trace = wrapper.framework_trace if trace.empty?

{ error:
({ exception: exception.class, message: exception.message, trace: trace}.
merge!( exception.respond_to?(:annotated_source_code) && { annotated_source_code: exception.annoted_source_code } || {} ))
}
end
end
end
end
7 changes: 5 additions & 2 deletions lib/logstasher/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rails/railtie'
require 'action_view/log_subscriber'
require 'action_controller/log_subscriber'
require 'logstasher/rails_ext/rack/debug_exceptions'
require 'socket'

module LogStasher
Expand Down Expand Up @@ -33,8 +34,10 @@ class Railtie < Rails::Railtie
LogStasher.setup_before(app.config.logstasher) if app.config.logstasher.enabled
end

initializer :logstasher do
config.after_initialize do
initializer :logstasher do |app|
app.config.middleware.use ::LogStasher::ActionDispatch::DebugExceptions
app.config.middleware.delete ::ActionDispatch::DebugExceptions if Rails.env.production?
app.config.after_initialize do
LogStasher.setup(config.logstasher) if config.logstasher.enabled
end
end
Expand Down

0 comments on commit 2738115

Please sign in to comment.