LoginSignup
41
41

More than 5 years have passed since last update.

Rack middleware stack のデバッグ方法

Posted at

この middleware ちゃんと動いてるか確認したい、とか、この middleware とこの middleware を同時に使うとなんかヘッダがおかしいけどどうなってるの!とか、そういうののデバッグ法。

単にログ吐く middleware を stack の途中に積めばよい。 @app.call の前後でそれぞれロギングできる。

config/environments/development.rb
  class M
    def initialize(app)
      @app = app
    end
    def call(env)
      warn '='*80
      env.each do |k,v|
        warn "#{k}=#{v}"
      end

      warn '='*80
      triplet = @app.call(env)
      warn triplet.inspect
      triplet
    end
  end

  config.middleware.insert_before Rack::ETag, M
  config.middleware.insert_after Rack::ETag, M

一個作れば何度でも積み込めるので任意の位置に差し込んでうまく使うと労力少なめでデバッグできる。

$ rake middleware
use Rack::Timeout
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fd6524e9a28>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use BetterErrors::Middleware
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use Dragonfly::CookieMonster
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use ActionDispatch::Head
use Rack::ConditionalGet
use M
use Rack::ETag
use M
use ActionDispatch::BestStandardsSupport
use Rack::Cors
use MetaRequest::Middlewares::MetaRequestHandler
use MetaRequest::Middlewares::Headers
use MetaRequest::Middlewares::AppRequestHandler
run MyApp::Application.routes
41
41
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
41