Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
attempt to change out ActionController::LogSubscriber in rails 3
#put this in config/initializers/custom_ac_log_subscriber.rb
## needed if we re-enable additions
#
#require 'active_support/core_ext/object/blank'
module MyApp
#TODO: we should no longer interit from ActionController::LogSubscriber
# as we are not truly unregistering the parent.
class CustomAcLogSubscriber < ActiveSupport::LogSubscriber
INTERNAL_PARAMS = ActionController::LogSubscriber::INTERNAL_PARAMS
def start_processing(event)
payload = event.payload
params = payload[:params].except(*INTERNAL_PARAMS)
format_or = payload[:formats].first.to_s.upcase || "-"
info "Processing #{payload[:controller]}##{payload[:action]} to #{format_or} (for ? at ?) [#{payload[:method]}] X-PachubeRequestId: notpopulated"
info " Parameters: #{params.inspect}" unless params.empty?
end
def process_action(event)
payload = event.payload
additions = ActionController::Base.log_process_action(payload)
status_code = payload[:status] || "-"
status_word = Rack::Utils::HTTP_STATUS_CODES[payload[:status]] || "-"
message = "Completed in %.0fms (View: 0, DB: 0) | #{status_code} #{status_word} [#{payload[:path]}]" % event.duration
#These "Additions" would be very nice to have,
# but our existing log format doesn't support them
#
#message << " (#{additions.join(" | ")})" unless additions.blank?
info(message)
end
def logger
ActionController::Base.logger
end
end
end
#Failed approach:
#unregister the default LogSubscriber - our inherited one will do all.
#ActiveSupport::LogSubscriber.log_subscribers.delete_if{ |ls|
# ls.instance_of? ActionController::LogSubscriber }
#Working approach:
#Clear subscription from notifications for all of
# * process_action.action_controller
# * start_processing.action_controller
#Note that this undesireably unregisters everybody's interest in these,
#not just that of ActionController::LogSubscriber
#but we're unregistering everybody.
%w(process_action start_processing).each do |evt|
ActiveSupport::Notifications.unsubscribe "#{evt}.action_controller"
end
#register our one
MyApp::CustomAcLogSubscriber.attach_to :action_controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.