Disable Rails SQL logging in console
To curb those chatty SQL outputs in Rails, up the ActiveRecord's logger to ERROR
:
This reins in the verbosity to focus mainly on errors.
How to temporarily disable SQL logging
When you need a temporary break from SQL logs—maybe for debugging—you can cocoon your code in a block:
This maneuver lets you enjoy quiet during operations and resume normal logs afterwards.
Navigating the silence
Resuming logging without a hitch
Here's how to get the logs back (spoiler alert: it's pretty straightforward):
With this neat little trick, you can slip in and out of silence like a ninja.
Commanding the log level
Control the chatter with the logger's level:
Just like adjusting volume, but for logs.
Configuring the logger in development
In config/environments/development.rb
, config.after_initialize
provides a nifty handle for logging:
Think of it as a personal butler managing your logs.
Avoiding a null faux-pas
Setting the logger to nil
might cause a mess; Rails can be a drama queen if expected items go missing. To avoid logger.warn
errors:
This safely reroutes logs to the belly of the beast (i.e., Unix blackhole), keeping Rails drama-free.
Making ActiveSupport::LogSubscriber go stealth-mode
When subtlety is key, try overriding ActiveSupport::LogSubscriber
. It's the SQL log raconteur that you never knew you could silence:
Mum's the word!
Splitting the logger
If you need to see other ActiveRecord logs but keep SQL hushed, consider cloning the logger:
With this, SQL logs can take a nap while the rest of the logs party on!
Disabling SQL logging in Rails 4
In Rails 4, disabling SQL logging is a breeze:
The SQL gods can finally rest...
Was this article helpful?