実行されたmiddlewareをロギングしたい
config/initializers
配下に下記のファイルを設置する。
# Trace each middleware module entry/exit. Freely adapted from
# https://github.com/DataDog/dd-trace-rb/issues/368 "Trace each middleware in the Rails stack"
module MiddlewareTracer
def call(env)
Rails.logger.debug { "MiddleWare: entry #{self.class.name}" }
status, headers, response = super(env)
Rails.logger.debug { "MiddleWare: #{self.class.name}, returning with status #{status}" }
[status, headers, response]
end
end
# Instrument the middleware stack after initialization so that we
# know the stack won't be changed afterwards.
Rails.configuration.after_initialize do
Rails.application.middleware.each do |middleware|
klass = middleware.klass
# There are a few odd middlewares that aren't classes.
klass.prepend(MiddlewareTracer) if klass.respond_to?(:prepend)
end
end
出力例
06:54:05 app.1 | MiddleWare: entry Webpacker::DevServerProxy
06:54:05 app.1 | MiddleWare: entry Rack::Cors
06:54:05 app.1 | MiddleWare: entry Raven::Rack
06:54:05 app.1 | MiddleWare: entry ActionDispatch::HostAuthorization
06:54:05 app.1 | MiddleWare: entry Rack::Sendfile
06:54:05 app.1 | MiddleWare: entry ActionDispatch::Static
...
06:54:05 app.1 | MiddleWare: ActionDispatch::Static, returning with status 400
06:54:05 app.1 | MiddleWare: Rack::Sendfile, returning with status 400
06:54:05 app.1 | MiddleWare: ActionDispatch::HostAuthorization, returning with status 400
06:54:05 app.1 | MiddleWare: Raven::Rack, returning with status 400
06:54:05 app.1 | MiddleWare: Rack::Cors, returning with status 400
06:54:05 app.1 | MiddleWare: Webpacker::DevServerProxy, returning with status 400
ruby on rails - Trace Error in Rack Middleware - Stack Overflow