aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/abstract_controller/logger.rb
blob: ebdeab3dfb3a76e5babc8d0d196e6bb41b8cf651 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'active_support/core_ext/logger'
require 'active_support/benchmarkable'

module AbstractController
  module Logger
    extend ActiveSupport::Concern

    included do
      cattr_accessor :logger
      extend ActiveSupport::Benchmarkable
    end

    # 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 < ActiveSupport::BasicObject
      def initialize(&block)
        @str, @block = nil, block
      end

      def method_missing(*args, &block)
        unless @str
          @str, @block = @block.call, nil
        end
        @str.send(*args, &block)
      end
    end

    # Override process_action in the AbstractController::Base
    # to log details about the method.
    def process_action(action)
      result = ActiveSupport::Orchestra.instrument(:process_action,
                :controller => self, :action => action) do
        super
      end

      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

      result
    end

  private
    # Returns the request origin with the IP and time. This needs to be cached,
    # otherwise we would get different results for each time it calls.
    def request_origin
      @request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
    end
  end
end