Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress Rails logging for specific ActiveRecord request

I know that Rails logging could be suppressed for entire controller using the silencer gem. But could an SQL output of some ActiveRecord request be suppressed individually by wrapping it into some statements?

like image 434
Paul Avatar asked Oct 22 '25 04:10

Paul


2 Answers

Silence

I don't have a specific answer for your question, but we use ActiveRecord::Base.logger.silence to silence the logger:

    ActiveRecord::Base.logger.silence do
        # your method here
    end

It's not for specific requests, but works for actions you may wish to stop logging. Perhaps you could extend ActiveRecord::Base to silence a specific method?

--

Extending ActiveRecord

Perhaps you could mix what I've described with extending ActiveRecord? The recommendation on this question is to use an ActiveRecord concern.

I've never used one of these - can put together some code if you feel applicable

like image 120
Richard Peck Avatar answered Oct 23 '25 21:10

Richard Peck


I use this:

save_for_later = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil #turns off sql activity
... do stuff...
ActiveRecord::Base.logger = save_for_later

I've never done this in production code btw, only in rake tasks...

like image 44
pixelearth Avatar answered Oct 23 '25 22:10

pixelearth