aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/abstract_controller/logger.rb
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-08-06 22:51:24 -0300
committerYehuda Katz <wycats@gmail.com>2009-08-06 22:51:24 -0300
commit71638e6760bed0445e5fefc185924b07076fef47 (patch)
tree280bf8b9669322d1618b1c5a0a432d3eb0bd5fa3 /actionpack/lib/abstract_controller/logger.rb
parent16c01224cbe503ab1f4d0f02955b16662be00116 (diff)
downloadrails-71638e6760bed0445e5fefc185924b07076fef47.tar.gz
rails-71638e6760bed0445e5fefc185924b07076fef47.tar.bz2
rails-71638e6760bed0445e5fefc185924b07076fef47.zip
Move AbstractController to a top-level component
Diffstat (limited to 'actionpack/lib/abstract_controller/logger.rb')
-rw-r--r--actionpack/lib/abstract_controller/logger.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/actionpack/lib/abstract_controller/logger.rb b/actionpack/lib/abstract_controller/logger.rb
new file mode 100644
index 0000000000..fd33bd2ddd
--- /dev/null
+++ b/actionpack/lib/abstract_controller/logger.rb
@@ -0,0 +1,52 @@
+require 'active_support/core_ext/logger'
+
+module AbstractController
+ module Logger
+ extend ActiveSupport::Concern
+
+ # A class that allows you to defer expensive processing
+ # until the logger actually tries to log. Otherwise, you are
+ # forced to do the processing in advance, and send the
+ # entire processed String to the logger, which might
+ # just discard the String if the log level is too low.
+ #
+ # TODO: Require that Rails loggers accept a block.
+ class DelayedLog
+ def initialize(&blk)
+ @blk = blk
+ end
+
+ def to_s
+ @blk.call
+ end
+ alias to_str to_s
+ end
+
+ included do
+ cattr_accessor :logger
+ end
+
+ # Override process_action in the AbstractController::Base
+ # to log details about the method.
+ def process_action(action)
+ super
+
+ if logger
+ log = DelayedLog.new do
+ "\n\nProcessing #{self.class.name}\##{action_name} " \
+ "to #{request.formats} " \
+ "(for #{request_origin}) [#{request.method.to_s.upcase}]"
+ end
+
+ logger.info(log)
+ end
+ end
+
+ private
+ def request_origin
+ # this *needs* to be cached!
+ # otherwise you'd get different results if calling it more than once
+ @request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
+ end
+ end
+end