From 04ed8b2a0c322ff122c3b9f3ffbd4bfb6df6d677 Mon Sep 17 00:00:00 2001 From: Marc Grimme Date: Sat, 25 Jun 2016 11:52:32 +0200 Subject: [PATCH] Overwrite middleware to handle errors. --- .../rails_ext/rack/debug_exceptions.rb | 26 +++++++++++++++++++ lib/logstasher/railtie.rb | 7 +++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 lib/logstasher/rails_ext/rack/debug_exceptions.rb diff --git a/lib/logstasher/rails_ext/rack/debug_exceptions.rb b/lib/logstasher/rails_ext/rack/debug_exceptions.rb new file mode 100644 index 0000000..e10ef6f --- /dev/null +++ b/lib/logstasher/rails_ext/rack/debug_exceptions.rb @@ -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 diff --git a/lib/logstasher/railtie.rb b/lib/logstasher/railtie.rb index 79e5363..fe6b8ad 100644 --- a/lib/logstasher/railtie.rb +++ b/lib/logstasher/railtie.rb @@ -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 @@ -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