In development, we store files using ActiveStorage's disk
service. This means that stored files are served by your Rails application, and every request to a file results in (at least!) one non-trivial log entry which can be annoying. Here is how to disable those log entries.
Example
Here is an example of what loading a single <img>
in an example application writes to the Rails log.
Started GET "/rails/active_storage/blobs/redirect/..." for ::1 at ...
Processing by ActiveStorage::Blobs::RedirectController#show as SVG
Parameters: ...
ActiveStorage::Blob Load (0.4ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" ...
Disk Storage (0.4ms) Generated URL for file at key: ... (http://localhost:3000/rails/active_storage/disk/...)
Redirected to http://localhost:3000/rails/active_storage/disk/...
Completed 302 Found in 18ms (ActiveRecord: 0.4ms (1 query, 0 cached) | GC: 13.3ms)
Started GET "/rails/active_storage/disk/..." for ::1 at ...
Processing by ActiveStorage::DiskController#show as SVG
Parameters: ...
Completed 200 OK in 1ms (ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.0ms)
I never care about anything from those requests. It's all happening inside ActiveStorage anyway, and I don't want its noise to distract me from looking at my application's logs.
Especially when rendering a page with many images, this produces an annoying amount of irrelevant rows in my Rails log.
Here are two options to reduce that noise.
Option 1: Silencing logging inside ActiveStorage controller actions
If you want to silence only ActiveStorage::Blob Load
or Generated URL for file at key
log entries, silence logging in any ActiveStorage controller action like so.
Rails.application.config.to_prepare do
ActiveStorage::BaseController.around_action do |_controller, block|
Rails.logger.silence(&block)
end
end
This will still log all requests from above, but at least avoid ActiveStorage's ActiveRecord loading or URL generation lines.
Option 2: Completely silencing logging for ActiveStorage routes
If you want to silence ActiveStorage requests completely, you need to replace the Rails::Rack::Logger
middleware. It's the one that writes the Started GET "/rails/active_storage/disk/..."
lines to your log.
This sounds harder than it is. We basically just want a version of it that silences logging for ActiveStorage routes, so we can write it as follows.
class RackLoggerWithSilencedActiveStorageRoutes < Rails::Rack::Logger
def call(env)
if env['PATH_INFO']&.start_with?("#{Rails.configuration.active_storage.routes_prefix}/")
Rails.logger.silence do
super
end
else
super
end
end
end
Rails.configuration.middleware.swap(Rails::Rack::Logger, RackLoggerWithSilencedActiveStorageRoutes)
Put that into an initializer or similar, and wrap with if Rails.env.development?
, if necessary.