2010-06-04 12:49:25 -04:00
|
|
|
require 'active_support/core_ext/time/conversions'
|
2011-10-19 16:39:11 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
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
|
2011-10-19 16:39:11 -04:00
|
|
|
def initialize(app, tags=nil)
|
|
|
|
@app, @tags = app, tags.presence
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2012-09-24 17:07:10 -04:00
|
|
|
if @tags && Rails.logger.respond_to?(:tagged)
|
2011-10-19 16:39:11 -04:00
|
|
|
Rails.logger.tagged(compute_tags(env)) { call_app(env) }
|
|
|
|
else
|
|
|
|
call_app(env)
|
|
|
|
end
|
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
|
|
|
|
2011-10-19 16:39:11 -04:00
|
|
|
def call_app(env)
|
2010-06-24 07:23:43 -04:00
|
|
|
request = ActionDispatch::Request.new(env)
|
2011-01-01 11:51:05 -05:00
|
|
|
path = request.filtered_path
|
2012-09-25 23:27:26 -04:00
|
|
|
Rails.logger.info "\n\n"
|
|
|
|
Rails.logger.info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
|
2011-10-19 16:39:11 -04:00
|
|
|
@app.call(env)
|
|
|
|
ensure
|
|
|
|
ActiveSupport::LogSubscriber.flush_all!
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2011-10-19 16:39:11 -04:00
|
|
|
def compute_tags(env)
|
|
|
|
request = ActionDispatch::Request.new(env)
|
|
|
|
|
|
|
|
@tags.collect do |tag|
|
|
|
|
case tag
|
|
|
|
when Proc
|
|
|
|
tag.call(request)
|
|
|
|
when Symbol
|
|
|
|
request.send(tag)
|
|
|
|
else
|
|
|
|
tag
|
|
|
|
end
|
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|