Easily Disable ActiveRecord SQL Logging Conditionally

06 Aug 2013

Sometimes reading through all the logging ActiveRecord does when running rails server and console can be a pain. It can muddle things up and make it pretty hard to debug an issue you may be having. This is a super easy trick to subjectively disable logging for your rails server or rails console. The main way of going about this is:

  # config/environment.rb
  ActiveRecord::Base.logger = Logger.new('/dev/null')

What this does is dump the logging from ActiveRecord into a null buffer never to be seen again. Yay! This is great but now everytime you want to allow sql logging you need to go back in and remove this line or comment it out. This is brittle and can lead to confusion if you or a coworker forgets you slapped this in your environment file. So the simple way around this is to wrap it in an environment variable like so:

  # config/environment.rb
  if ENV["NOSQL"]
    ActiveRecord::Base.logger = Logger.new('/dev/null')

Now whenever you want to disable SQL logging simply call:

  NOSQL=true rails s

and if you do want to have SQL logging just leave it off like normal and you are good to go:

  rails s

This will also work nice with foreman like so:

  NOSQL=true foreman start

Like I said, super easy right?!

comments powered by Disqus