aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack/logger.rb
blob: 73e9af3b41570bbe34f551ddea4359c8fe179327 (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
require 'rails/log_subscriber'
require 'active_support/core_ext/time/conversions'

module Rails
  module Rack
    # Log the request started and flush all loggers after it.
    class Logger < Rails::LogSubscriber
      def initialize(app)
        @app = app
      end

      def call(env)
        before_dispatch(env)
        @app.call(env)
      ensure
        after_dispatch(env)
      end

      protected

        def before_dispatch(env)
          request = ActionDispatch::Request.new(env)
          path = request.fullpath

          info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \
               "for #{request.ip} at #{Time.now.to_default_s}"
        end

        def after_dispatch(env)
          Rails::LogSubscriber.flush_all!
        end

    end
  end
end