forked from shadabahmed/logstasher
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overwrite middleware to handle errors in logstash compatible format.
Closes shadabahmed#79
- Loading branch information
Marc Grimme
committed
Jun 30, 2016
1 parent
8e7d5df
commit dc69286
Showing
4 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'action_dispatch' | ||
require 'active_support/all' | ||
|
||
module LogStasher | ||
module ActionDispatch | ||
def build_exception_hash(wrapper) | ||
exception = wrapper.exception | ||
trace = wrapper.application_trace | ||
trace = wrapper.framework_trace if trace.empty? | ||
|
||
{ error: | ||
({ exception: exception.class.name, message: exception.message, trace: trace}. | ||
merge!( exception.respond_to?(:annotated_source_code) && { annotated_source_code: exception.annoted_source_code } || {} )) | ||
} | ||
end | ||
|
||
class DebugExceptions < ::ActionDispatch::DebugExceptions | ||
include ::LogStasher::ActionDispatch | ||
|
||
private | ||
def log_error(env, wrapper) | ||
LogStasher.logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n" | ||
end | ||
|
||
end | ||
|
||
class TwoWayDebugExceptions < ::ActionDispatch::DebugExceptions | ||
include ::LogStasher::ActionDispatch | ||
|
||
private | ||
def log_error(env, wrapper) | ||
LogStasher.logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n" | ||
|
||
super(env, wrapper) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
spec/lib/logstasher/rails_ext/rack/debug_exceptions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
shared_examples 'MyApp' do | ||
before do | ||
class MyApp | ||
def initialize() | ||
end | ||
def call(*args) | ||
raise Exception.new("My Exception") | ||
end | ||
end | ||
end | ||
|
||
let(:app) { MyApp.new } | ||
let(:environment) { { 'action_dispatch.show_exceptions' => true } } | ||
let(:logger) { double } | ||
subject{ described_class.new(app) } | ||
|
||
before(:each) do | ||
allow(LogStasher).to receive(:logger).and_return(logger) | ||
allow(LogStasher.logger).to receive(:'<<').and_return(true) | ||
end | ||
end | ||
|
||
shared_examples 'build_exception_hash' do | ||
describe '#build_exception_hash' do | ||
let (:wrapper) { double(exception: Exception.new("My Exception"), application_trace: [ "line5" ]) } | ||
it do | ||
hash = subject.build_exception_hash(wrapper) | ||
|
||
expect(hash).to match({:error=>{:exception=>"Exception", :message=>"My Exception", :trace=>["line5"]}}) | ||
end | ||
end | ||
end | ||
|
||
describe ::LogStasher::ActionDispatch::DebugExceptions do | ||
include_examples 'MyApp' | ||
|
||
describe 'calls LogStasher.logger with json format exception' do | ||
describe '#log_error' do | ||
it do | ||
expect(LogStasher).to receive(:build_logstash_event) | ||
expect(LogStasher.logger).to receive(:'<<').and_return(true) | ||
expect{ subject.call(environment) }.to raise_error(Exception, "My Exception") | ||
end | ||
end | ||
end | ||
|
||
include_examples 'build_exception_hash' | ||
end | ||
|
||
describe ::LogStasher::ActionDispatch::TwoWayDebugExceptions do | ||
include_examples 'MyApp' | ||
|
||
let(:logger_2nd) { double } | ||
before(:each) do | ||
allow(subject).to receive(:logger).and_return(logger_2nd) | ||
end | ||
|
||
describe 'calls LogStasher.logger with json format exception' do | ||
describe '#log_error' do | ||
it do | ||
expect(LogStasher).to receive(:build_logstash_event) | ||
expect(LogStasher.logger).to receive(:'<<').and_return(true) | ||
expect(logger_2nd).to receive(:fatal).and_return(true) | ||
expect{ subject.call(environment) }.to raise_error(Exception, "My Exception") | ||
end | ||
end | ||
end | ||
|
||
include_examples 'build_exception_hash' | ||
end |