Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No such middleware to insert before: Rails::Rack::Logger #26

Open
DarkArc opened this issue Jun 21, 2018 · 7 comments
Open

No such middleware to insert before: Rails::Rack::Logger #26

DarkArc opened this issue Jun 21, 2018 · 7 comments

Comments

@DarkArc
Copy link

DarkArc commented Jun 21, 2018

Seeing the same issue as reported in: #24 and various others.

No such middleware to insert before: Rails::Rack::Logger

We were previously on 5.0 with no issues even configuring this inside of the environment files. However, after migrating to 5.2, we experience this issue regardless of where the middleware swap is performed (initializer or environment file).

The potential work around in #25 also does nothing to help this situation.

@eashman
Copy link

eashman commented Aug 27, 2018

Seeing this problem as well on 5.2. Any help or suggestions of other methods to suppress health check pings from AWS Load Balancers in the logs would be appreciated.

@DarkArc
Copy link
Author

DarkArc commented Aug 28, 2018

Changed employers, no longer have access to the source code of the application that was causing the issue.

FWIW: A potentially solid workaround is to use a logger that has the ability to be passed a filter function, ex: Semantic Logger. Then you can stop things from being logged at the logger level.

@cheald
Copy link

cheald commented Nov 12, 2019

I had this problem show up suddenly after upgrading from webpacker 3 to 4.

Changing swap to insert_before seems to work just fine, though:

Rails.application.middleware.insert_before(
  Rails::Rack::Logger,
  Silencer::Logger,
  ...)

@AlexWayfer
Copy link

We use rails_semantic_logger, and it replaces Rails::Rack::Logger by itself version (but only when config.rails_semantic_logger.semantic is true, which can be different for different environments), so this code pretty works:

config.middleware.swap(
  config.rails_semantic_logger.semantic ? RailsSemanticLogger::Rack::Logger : Rails::Rack::Logger,
  Silencer::Logger,
  # ...
)

@bjorntrondsen
Copy link

I ran into this when upgrading an app from Rails 6.1 to Rails 7.0.
config.middleware.swap triggers the "No such middleware to insert before" exception.
Achieving the same thing in two steps with insert_before and delete works though for some reason:

config/initializers/silencer.rb

require 'silencer/rails/logger'

Rails.application.configure do
  config.middleware.insert_before(
    Rails::Rack::Logger,
    Silencer::Logger,
    config.log_tags,
    silence: ["/noisy/action"]
  )
  config.middleware.delete Rails::Rack::Logger
end

@pboling
Copy link

pboling commented Apr 15, 2023

Running into this same error in an app I am upgrading in phases, which is currently at Rails 5.2 and Ruby 2.7. I was hitting it with silencer v1, and then tried upgrading to v2, but still getting the same error.

Using the insert_before followed by delete hack posted by @bjorntrondsen did not work for me.

Turns out that the issue was entirely a red herring error. The app was failing somewhere else in a way that was recovered somehow, but resulted in Rails not being fully loaded, and thus the Rails::Rack::Logger hadn't been defined.

In my case, this was due to rubocop-rails (run with -A to get the "unsafe" fixes) changing instances of:

class MyRecord < ActiveRecord::Base

to

class MyRecord < ApplicationRecord

despite ApplicationRecord not existing in my app. I defined ApplicationRecord in app/models/application_record.rb as:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

And all of a sudden everything is happy again. The original setup code from the readme has started working again!

@coldnebo
Copy link

I ran into this when upgrading an app from Rails 6.1 to Rails 7.0. config.middleware.swap triggers the "No such middleware to insert before" exception. Achieving the same thing in two steps with insert_before and delete works though for some reason

I had the same problem and breaking it into two instructions as you did worked for me.

Looking at the implementation of middleware stack in Rails, I notice that the problematic methods delete_at, swap, all use index_of, where as delete, use, and insert_before (an alias of insert) don't use index_of.

But I don't see a problem with that code on the face of it, unless something about that array is immutable or being accessed from separate threads on startup. Is rails startup async now? I saw some concurrent ruby discussions in the official Rails guides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants