2010-06-04 12:49:25 -04:00
|
|
|
require 'active_support/core_ext/time/conversions'
|
2010-01-21 07:05:30 -05:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Rack
|
|
|
|
# Log the request started and flush all loggers after it.
|
2010-06-24 07:23:43 -04:00
|
|
|
class Logger < ActiveSupport::LogSubscriber
|
2010-01-21 07:05:30 -05:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2010-01-26 09:37:45 -05:00
|
|
|
before_dispatch(env)
|
|
|
|
@app.call(env)
|
|
|
|
ensure
|
|
|
|
after_dispatch(env)
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
protected
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
def before_dispatch(env)
|
|
|
|
request = ActionDispatch::Request.new(env)
|
|
|
|
path = request.fullpath
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \
|
|
|
|
"for #{request.ip} at #{Time.now.to_default_s}"
|
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
def after_dispatch(env)
|
|
|
|
ActiveSupport::LogSubscriber.flush_all!
|
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|