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.
Added a new middleware that adds json logs for exceptions. Plus silen…
…ce the old logs if suppress_app_logs. Closes shadabahmed#79
- Loading branch information
Marc Grimme
committed
Jul 6, 2016
1 parent
ab78ea0
commit 7b91d94
Showing
6 changed files
with
145 additions
and
16 deletions.
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
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,45 @@ | ||
require 'action_dispatch' | ||
require 'active_support/all' | ||
|
||
module LogStasher | ||
module ActionDispatch | ||
|
||
class DebugExceptions < ::ActionDispatch::DebugExceptions | ||
include ::LogStasher::ActionDispatch | ||
|
||
def initialize(app, routes_app = nil) | ||
@app = app | ||
@routes_app = routes_app | ||
end | ||
|
||
def call(env) | ||
begin | ||
status, header, body = @app.call(env) | ||
if header['X-Cascade'] == 'pass' | ||
raise ::ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}" | ||
end | ||
rescue Exception => exception | ||
log_error(env, ::ActionDispatch::ExceptionWrapper.new(env, exception)) | ||
end | ||
[status, header, body] | ||
end | ||
|
||
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 | ||
|
||
private | ||
|
||
def log_error(env, wrapper) | ||
LogStasher.logger << LogStasher.build_logstash_event(build_exception_hash(wrapper), ["exception"]).to_json + "\n" | ||
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
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
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 | ||
|
||
describe ::LogStasher::ActionDispatch::DebugExceptions do | ||
include_examples 'MyApp' | ||
|
||
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 | ||
|
||
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) | ||
subject.call(environment) | ||
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