Add splunk logging for errors

This commit is contained in:
Raphael 2011-01-27 11:44:17 -08:00
parent 0fa0b7e950
commit a6f8b2c14e
3 changed files with 57 additions and 0 deletions

View file

@ -11,6 +11,23 @@ class ActionView::LogSubscriber
alias :render_collection :render_template
end
module ActionDispatch
class ShowExceptions
private
def log_error(exception)
return unless logger
ActiveSupport::Deprecation.silence do
message = "event=error error_class=#{exception.class} error_message='#{exception.message}' "
message << "orig_error_message='#{exception.original_exception.message}'" if exception.respond_to?(:original_exception)
message << "annotated_source='#{exception.annoted_source_code.to_s}' " if exception.respond_to?(:annoted_source_code)
message << "app_backtrace='#{application_trace(exception).join(";")}'"
logger.fatal("\n\n#{message}\n\n")
end
end
end
end
class ActionController::LogSubscriber
def start_processing(event)
#noop

View file

@ -31,6 +31,19 @@ describe AspectsController do
it_should_behave_like "it overrides the logs on success"
end
describe "custom logging on error" do
class FakeError < RuntimeError; attr_accessor :original_exception; end
before do
@action = :index
@desired_error_message = "I love errors"
@error = FakeError.new(@desired_error_message)
@orig_error_message = "I loooooove nested errors!"
@error.original_exception = NoMethodError.new(@orig_error_message)
@controller.stub(:index).and_raise(@error)
end
it_should_behave_like "it overrides the logs on error"
end
describe "custom logging on redirect" do
before do
@action = :show

View file

@ -1,12 +1,18 @@
class FakeLogger
attr_accessor :infos
attr_accessor :lines
def initialize
self.infos = []
self.lines = []
end
def info line
self.infos << line
self.lines << line
end
def fatal line
self.lines << line
end
end
@ -51,6 +57,27 @@ shared_examples_for 'it overrides the logs on success' do
end
end
shared_examples_for 'it overrides the logs on error' do
before do
Rails.stub(:logger).and_return(FakeLogger.new)
begin
get @action, @action_params
rescue Exception => e
ActionDispatch::ShowExceptions.new(nil).send(:log_error,e)
end
@line = Rails.logger.lines.last
end
it 'logs the backtrace' do
@line.should =~ /app_backtrace=/
end
it 'logs the error message' do
@line.should =~ /error_message='#{@desired_error_message}'/
end
it 'logs the original error message, if it exists' do
@line.should =~ /orig_error_message='#{@orig_error_message}'/ if @orig_error_message
end
end
shared_examples_for 'it overrides the logs on redirect' do
before do
Rails.stub(:logger).and_return(FakeLogger.new)