Created
July 16, 2013 17:18
-
-
Save tedgrubb/6010687 to your computer and use it in GitHub Desktop.
ActiveResource LogSubscriber exception logging instead of breaking on nil result. Fixes undefined method 'code' for NilClass when ActiveResource throws a TimeoutError.
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
module ActiveResource | |
class LogSubscriber < ActiveSupport::LogSubscriber | |
def request(event) | |
result = event.payload[:result] | |
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}" | |
if event.payload.has_key?(:exception) | |
error "#{event.payload[:exception].join(' ')}\n" | |
else | |
info "STATUS %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration] | |
info "BODY #{result.body}\n" | |
end | |
end | |
def logger | |
ActiveResource::Base.logger | |
end | |
end | |
end | |
# This is already called in /active_resource/log_subscriber.rb | |
# ActiveResource::LogSubscriber.attach_to :active_resource | |
# Attach to STDOUT | |
ActiveResource::Base.logger = Logger.new(STDOUT) | |
ActiveResource::Base.logger.level = Logger::DEBUG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment