aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/abstract/logger.rb
blob: d0603a4ad7f1d674284db89f5351aa750ec1f13c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module AbstractController
  module Logger
    extend ActiveSupport::DependencyModule

    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
    
    def process(action)
      ret = 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
      
      ret
    end
    
    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