Skip to content

Commit

Permalink
Silence debugbar routes from the logs
Browse files Browse the repository at this point in the history
introduce QuietRoutes middleware
  • Loading branch information
julienbourdeau committed Feb 26, 2024
1 parent 8b262be commit efe491b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 0 additions & 1 deletion config/routes.rb
Expand Up @@ -5,6 +5,5 @@
options 'poll/confirm' => "polling#confirm"
post 'poll/confirm' => "polling#confirm"

# TODO: Silence logs for this route if `::Rails.application.config.assets.quiet` is true
get 'assets/script' => "assets#js"
end
2 changes: 2 additions & 0 deletions lib/debugbar/engine.rb
@@ -1,4 +1,5 @@
require_relative 'config'
require_relative 'middlewares/quiet_routes'
require_relative 'middlewares/track_current_request'
require_relative '../../app/helpers/debugbar/tag_helpers'

Expand Down Expand Up @@ -38,6 +39,7 @@ class Engine < ::Rails::Engine
initializer 'debugbar.inject_middlewares' do |app|
next unless Debugbar.config.enabled?
app.middleware.insert_after ActionDispatch::Executor, Debugbar::TrackCurrentRequest
app.middleware.insert_after Sprockets::Rails::QuietAssets, Debugbar::QuietRoutes
end

initializer 'debugbar.subscribe' do
Expand Down
23 changes: 23 additions & 0 deletions lib/debugbar/middlewares/quiet_routes.rb
@@ -0,0 +1,23 @@
# This middleware silences the Rails logger for requests to the Debugbar routes.
# The poll route can be *very* noisy.
# Rails already does this for the /assets route, see Sprockets::Rails::QuietAssets.
#
# @see Sprockets::Rails::QuietAssets
# @see Rails::Rack::Logger#silence
#
module Debugbar
class QuietRoutes
def initialize(app)
@app = app
@route_regex = %r(\A/{0,2}#{::Debugbar.config.prefix})
end

def call(env)
if env['PATH_INFO'] =~ @route_regex
::Rails.logger.silence { @app.call(env) }
else
@app.call(env)
end
end
end
end

0 comments on commit efe491b

Please sign in to comment.