Disable Rails SQL logging in console

SqlRuby on-RailsConsole

Sql Problem Overview


Is there a way to disable SQL query logging when I'm executing commands in the console? Ideally, it would be great if I can just disable it and re-enable it with a command in the console.

I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read.


Edit: I found another solution, since setting the logger to nil sometimes raised an error, if something other than my code tried to call logger.warn

Instead of setting the logger to nil you can set the level of the logger to 1.

ActiveRecord::Base.logger.level = 1 # or Logger::INFO

Sql Solutions


Solution 1 - Sql

To turn it off:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

To turn it back on:

ActiveRecord::Base.logger = old_logger

Solution 2 - Sql

Here's a variation I consider somewhat cleaner, that still allows potential other logging from AR. In config/environments/development.rb :

config.after_initialize do
  ActiveRecord::Base.logger = Rails.logger.clone
  ActiveRecord::Base.logger.level = Logger::INFO
end

Solution 3 - Sql

This might not be a suitable solution for the console, but Rails has a method for this problem: Logger#silence

ActiveRecord::Base.logger.silence do
  # the stuff you want to be silenced
end

Solution 4 - Sql

For Rails 4 you can put the following in an environment file:

# /config/environments/development.rb

config.active_record.logger = nil

Solution 5 - Sql

In case someone wants to actually knock out SQL statement logging (without changing logging level, and while keeping the logging from their AR models):

The line that writes to the log (in Rails 3.2.16, anyway) is the call to debug in lib/active_record/log_subscriber.rb:50.

That debug method is defined by ActiveSupport::LogSubscriber.

So we can knock out the logging by overwriting it like so:

module ActiveSupport
  class LogSubscriber
    def debug(*args, &block)
    end
  end
end

Solution 6 - Sql

I used this: config.log_level = :info edit-in config/environments/performance.rb

Working great for me, rejecting SQL output, and show only rendering and important info.

Solution 7 - Sql

In Rails 3.2 I'm doing something like this in config/environment/development.rb:

module MyApp
  class Application < Rails::Application
    console do
      ActiveRecord::Base.logger = Logger.new( Rails.root.join("log", "development.log") )
    end
  end
end

Solution 8 - Sql

Just as an FYI, in Rails 2 you can do

ActiveRecord::Base.silence { <code you don't want to log goes here> }

Obviously the curly braces could be replaced with a do end block if you wanted.

Solution 9 - Sql

I had to solve this for ActiveRecord 6, and I based my answer on fakeleft's response, but it wasn't quite right, since it was suppressing other logging such as the logging of nested views. What I did was created config/initializers/activerecord_logger.rb:

# Suppress SQL statement logging if necessary
# This is a dirty, dirty trick, but it works:
if ENV["ACTIVERECORD_HIDE_SQL"].present?
  module ActiveRecord
    class LogSubscriber
      def sql(event)
      end
    end
  end
end

The log subscriber in AR 6 has a sql event that we want to hide, so this is very narrowly targeted to skip that event.

Solution 10 - Sql

I use activerecord 6.0.3.3 and I had to include ActiveSupport::LoggerSilence

include ActiveSupport::LoggerSilence

ActiveSupport::LoggerSilence.silence do
    ## everything you want to silence
end

This however did not work with anything related to creating or deleting SQL tables like ActiveRecord::Migration.drop_table. For this to be silenced I added:

ActiveRecord::Schema.verbose = false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongylazView Question on Stackoverflow
Solution 1 - SqlRyan BiggView Answer on Stackoverflow
Solution 2 - SqljrochkindView Answer on Stackoverflow
Solution 3 - SqlChristoph PetschnigView Answer on Stackoverflow
Solution 4 - SqlMicahView Answer on Stackoverflow
Solution 5 - SqlfakeleftView Answer on Stackoverflow
Solution 6 - SqlNate BenView Answer on Stackoverflow
Solution 7 - SqlTelmo CostaView Answer on Stackoverflow
Solution 8 - SqlMax WilliamsView Answer on Stackoverflow
Solution 9 - SqlChris RView Answer on Stackoverflow
Solution 10 - SqlnwnollView Answer on Stackoverflow