-
-
Save slyphon/2582094 to your computer and use it in GitHub Desktop.
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
# | |
# Ideas stolen from lograge and brought to Rails 2.3 | |
# https://github.com/mattmatt/lograge/blob/master/lib/lograge/log_subscriber.rb | |
# | |
module ImprovedControllerLogging | |
def self.included(base) | |
base.alias_method_chain :log_processing, :fixup | |
base.inject_alias_method_chain :perform_action, | |
:perform_action_with_benchmark, | |
:perform_action_with_fixed_benchmark | |
end | |
def log_processing_with_fixup | |
# We aren't going to log at the start of the request | |
end | |
def perform_action_with_fixed_benchmark | |
if logger | |
ms = [Benchmark.ms { perform_action_without_benchmark }, 0.01].max | |
parameters = respond_to?(:filter_parameters) ? filter_parameters(params) : params.dup | |
parameters = parameters.except('controller', 'action', 'format', '_method') | |
payload = { | |
:controller => self.class.name, | |
:action => self.action_name, | |
:params => parameters, | |
:format => request.format.to_sym, | |
:method => request.method, | |
:path => (request.fullpath rescue "unknown"), | |
:status => response.status.to_s[0..2] | |
} | |
full_action = "#{params[:controller]}##{params[:action]}" | |
message = "#{payload[:method].to_s.upcase} #{payload[:path]} action=#{full_action} format=#{payload[:format]}" | |
message << " status=#{payload[:status]}" | |
message << " duration=#{ms.to_i}ms" | |
logging_view = defined?(@view_runtime) | |
logging_active_record = Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? | |
if logging_active_record | |
db_runtime = ActiveRecord::Base.connection.reset_runtime | |
db_runtime += @db_rt_before_render if @db_rt_before_render | |
db_runtime += @db_rt_after_render if @db_rt_after_render | |
message << " db=#{db_runtime.to_i}ms" | |
end | |
if logging_view | |
message << " view=#{@view_runtime.to_i}ms" | |
end | |
unless parameters.empty? | |
message << " params=#{parameters.inspect}" | |
end | |
logger.info(message) | |
response.headers["X-Runtime"] = "%.0f" % ms | |
else | |
perform_action_without_benchmark | |
end | |
end | |
end | |
class Module | |
def inject_alias_method_chain(target, method_to_replace, method_to_use) | |
method_instance_to_replace = instance_method(method_to_replace) | |
meths = (private_instance_methods | instance_methods).sort.grep(/^#{target}/) | |
found = meths.detect do |m| | |
m != method_to_replace.to_s && instance_method(m) == method_instance_to_replace | |
end | |
if found | |
alias_method found, method_to_use | |
end | |
end | |
end | |
ActionController::Base.class_eval do | |
include ImprovedControllerLogging | |
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
GET /welcome action=welcome#index format=html status=200 duration=158ms db=0ms view=26ms |
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
# Activate improved controller logging | |
config.after_initialize do | |
require 'improved_controller_logging' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment