2009-05-18 19:15:43 -04:00
|
|
|
require 'active_support/core_ext/logger'
|
2009-05-14 04:56:07 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
module AbstractController
|
|
|
|
module Logger
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-07 11:29:22 -04:00
|
|
|
|
2009-06-09 20:03:02 -04:00
|
|
|
# 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.
|
2009-05-14 20:25:10 -04:00
|
|
|
class DelayedLog
|
|
|
|
def initialize(&blk)
|
|
|
|
@blk = blk
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-14 20:25:10 -04:00
|
|
|
def to_s
|
|
|
|
@blk.call
|
|
|
|
end
|
|
|
|
alias to_str to_s
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
included do
|
2009-04-07 17:57:18 -04:00
|
|
|
cattr_accessor :logger
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-06-09 20:03:02 -04:00
|
|
|
# Override process_action in the AbstractController::Base
|
|
|
|
# to log details about the method.
|
|
|
|
def process_action(action)
|
2009-08-26 14:11:54 -04:00
|
|
|
retval = super
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-14 20:25:10 -04:00
|
|
|
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
|
2009-08-26 14:11:54 -04:00
|
|
|
|
|
|
|
retval
|
2009-05-14 20:25:10 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-06-09 20:03:02 -04:00
|
|
|
private
|
2009-05-14 20:25:10 -04:00
|
|
|
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)}"
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-05-14 04:56:07 -04:00
|
|
|
end
|