Fork me on GitHub

Mohammad AbuShady Software Developer

How to stop ActiveRecord logs temporarily in heavy queries

When I’m writing a heavy query rake task or just running something in the console that does a lot of updates, and i want to output something it gets lost in all the query logs output. So I looked around for a convinent way to temporarily stop the output for just this method, then I found that you could access the active record logger object through ActiveRecord::Base.logger object, and it has many options that you can play with, here’s some options:

  • Completly silence logs in a block

  • Change logger level Logger has quite a few levels, and they are convieniently named with symbols that can be used.

    Qouted from the debugging rails guide, in the logger levels section

    The available log levels are: :debug, :info, :warn , :error , :fatal, and :unknown, corresponding to the log level numbers from 0 up to 5 respectively.

here’s how to change the values Unlike the silence method this doesn’t restore the old value after a blog, so if you plan to restore the old value then you need to save it in a variable before changing it