Skip to content

Commit 1576108

Browse files
author
Marc Grimme
committed
Overwrite middleware to handle errors in logstash compatible format.
1 parent 8e7d5df commit 1576108

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

lib/logstasher.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'logstasher/active_record/log_subscriber'
55
require 'logstasher/action_view/log_subscriber'
66
require 'logstasher/rails_ext/action_controller/base'
7+
require 'logstasher/rails_ext/rack/debug_exceptions'
78
require 'request_store'
89
require 'active_support/core_ext/module/attribute_accessors'
910
require 'active_support/core_ext/string/inflections'
@@ -137,6 +138,16 @@ def configured_to_suppress_app_logs?(config)
137138
!!(config.suppress_app_log.nil? ? config.supress_app_log : config.suppress_app_log)
138139
end
139140

141+
def modify_middleware(app)
142+
if enabled
143+
if configured_to_suppress_app_logs? app.config.logstasher
144+
app.middleware.swap ::ActionDispatch::DebugExceptions, ::LogStasher::ActionDispatch::DebugExceptions
145+
else
146+
app.middleware.swap ::ActionDispatch::DebugExceptions, ::LogStasher::ActionDispatch::TwoWayDebugExceptions
147+
end
148+
end
149+
end
150+
140151
def custom_fields
141152
Thread.current[:logstasher_custom_fields] ||= []
142153
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module LogStasher
2+
module ActionDispatch
3+
def build_exception_hash(wrapper)
4+
exception = wrapper.exception
5+
trace = wrapper.application_trace
6+
trace = wrapper.framework_trace if trace.empty?
7+
8+
{ error:
9+
({ exception: exception.class, message: exception.message, trace: trace}.
10+
merge!( exception.respond_to?(:annotated_source_code) && { annotated_source_code: exception.annoted_source_code } || {} ))
11+
}
12+
end
13+
14+
class DebugExceptions < ::ActionDispatch::DebugExceptions
15+
include ::LogStasher::ActionDispatch
16+
17+
private
18+
def log_error(env, wrapper)
19+
LogStasher.logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n"
20+
end
21+
22+
end
23+
24+
class TwoWayDebugExceptions < ::ActionDispatch::DebugExceptions
25+
include ::LogStasher::ActionDispatch
26+
27+
private
28+
def log_error(env, wrapper)
29+
LogStasher.logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n"
30+
31+
super(env, wrapper)
32+
end
33+
end
34+
end
35+
end

lib/logstasher/railtie.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Railtie < Rails::Railtie
2121
# Load and ERB templating of YAML files
2222
LOGSTASHER = File.exists?(config_file) ? YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
2323

24-
initializer :logstasher, :before => :load_config_initializers do |app|
24+
initializer :logstasher, before: :load_config_initializers do |app|
2525
if LOGSTASHER.present?
2626
# process common configs
2727
LogStasher.process_config(app.config.logstasher, LOGSTASHER)
@@ -38,6 +38,10 @@ class Railtie < Rails::Railtie
3838
LogStasher.setup(config.logstasher) if config.logstasher.enabled
3939
end
4040
end
41+
42+
initializer 'logstasher.insert_middleware', after: :load_config_initializers do |app|
43+
LogStasher.modify_middleware app
44+
end
4145
end
4246

4347
def process_config(config, yml_config)

0 commit comments

Comments
 (0)