2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
require "active_support/core_ext/time/conversions"
|
|
|
|
require "active_support/core_ext/object/blank"
|
|
|
|
require "active_support/log_subscriber"
|
|
|
|
require "action_dispatch/http/request"
|
|
|
|
require "rack/body_proxy"
|
2010-01-21 07:05:30 -05:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Rack
|
2012-09-26 18:08:25 -04:00
|
|
|
# Sets log tags, logs the request, calls the app, and flushes the logs.
|
2015-03-06 22:15:36 -05:00
|
|
|
#
|
2015-05-04 08:43:41 -04:00
|
|
|
# Log tags (+taggers+) can be an Array containing: methods that the +request+
|
|
|
|
# object responds to, objects that respond to +to_s+ or Proc objects that accept
|
|
|
|
# an instance of the +request+ object.
|
2010-06-24 07:23:43 -04:00
|
|
|
class Logger < ActiveSupport::LogSubscriber
|
2012-09-26 18:08:25 -04:00
|
|
|
def initialize(app, taggers = nil)
|
2013-01-09 18:34:36 -05:00
|
|
|
@app = app
|
|
|
|
@taggers = taggers || []
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2012-09-26 18:08:25 -04:00
|
|
|
request = ActionDispatch::Request.new(env)
|
|
|
|
|
2013-01-08 13:50:28 -05:00
|
|
|
if logger.respond_to?(:tagged)
|
|
|
|
logger.tagged(compute_tags(request)) { call_app(request, env) }
|
2011-10-19 16:39:11 -04:00
|
|
|
else
|
2012-09-26 18:08:25 -04:00
|
|
|
call_app(request, env)
|
2011-10-19 16:39:11 -04:00
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
private
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def call_app(request, env) # :doc:
|
|
|
|
instrumenter = ActiveSupport::Notifications.instrumenter
|
|
|
|
instrumenter.start "request.action_dispatch", request: request
|
|
|
|
logger.info { started_request_message(request) }
|
2018-04-03 22:54:27 -04:00
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
body = ::Rack::BodyProxy.new(body) { finish(request) }
|
|
|
|
[status, headers, body]
|
2016-12-28 21:53:51 -05:00
|
|
|
rescue Exception
|
|
|
|
finish(request)
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
ActiveSupport::LogSubscriber.flush_all!
|
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
# Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700
|
|
|
|
def started_request_message(request) # :doc:
|
|
|
|
'Started %s "%s" for %s at %s' % [
|
|
|
|
request.request_method,
|
|
|
|
request.filtered_path,
|
2018-07-31 10:39:42 -04:00
|
|
|
request.remote_ip,
|
2016-12-28 21:53:51 -05:00
|
|
|
Time.now.to_default_s ]
|
|
|
|
end
|
2011-10-19 16:39:11 -04:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def compute_tags(request) # :doc:
|
|
|
|
@taggers.collect do |tag|
|
|
|
|
case tag
|
|
|
|
when Proc
|
|
|
|
tag.call(request)
|
|
|
|
when Symbol
|
|
|
|
request.send(tag)
|
|
|
|
else
|
|
|
|
tag
|
|
|
|
end
|
2011-10-19 16:39:11 -04:00
|
|
|
end
|
|
|
|
end
|
2013-01-08 13:50:28 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def finish(request)
|
|
|
|
instrumenter = ActiveSupport::Notifications.instrumenter
|
|
|
|
instrumenter.finish "request.action_dispatch", request: request
|
|
|
|
end
|
2013-01-09 18:34:36 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def logger
|
|
|
|
Rails.logger
|
|
|
|
end
|
2010-01-21 07:05:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|