aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/lib/action_controller/abstract/logger.rb
blob: d6fa8434857e44c0e755a11c1e8481249fe3fb4b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                                           
                                        
 

                         
                                 
 



                          
 





                       
               
                            
       
 

                       
 








                                                                   
 

         
 



                                                                          
       
     
   
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/logger'

module AbstractController
  module Logger
    extend ActiveSupport::Concern

    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