1

This is what I have found out from another SO Answer Disable Rails SQL logging in console

ActiveRecord::Base.logger.silence do
  User.where(auth_token: request.headers['X-AUTH']).first
end

But the method seems to be deprecated. Is there any latest way to silence the logs for queries in a block?

gates
  • 4,465
  • 7
  • 32
  • 60
  • What's the class of your `ActiveRecord::Base.logger`? – SRack May 09 '18 at 16:42
  • 1
    ActiveRecord::Base.logger.class => ActiveSupport::Logger – gates May 09 '18 at 17:01
  • Huh - that docs for Rails 5.2 ([here](http://api.rubyonrails.org/classes/ActiveSupport/Logger.html) and [here](http://api.rubyonrails.org/classes/LoggerSilence.html)) show `silence` being included with no mention of it being deprecated. What version of Rails are you using? – SRack May 09 '18 at 17:04
  • Actually I am also not getting warning, but https://apidock.com/rails/Logger/silence showed deprecation. So I thought it's deprecated in 5.1 also. How do we know it's not deprecated? – gates May 09 '18 at 17:07

2 Answers2

0

The warning you're seeing here states:

Method deprecated or moved

This looks like a case of "or moved" :)

Within the warning block it mentions the locations of similar methods, including LoggerSilence#silence.

That module contains a silence method and is now included in the ActiveSupport::Logger class, which will handle it as you need.

Hope that helps here!

SRack
  • 11,495
  • 5
  • 47
  • 60
0

In Rails 6 I use the following to prevent a query log of a token:

def authenticate_user                                                                                                                                                                                                                                                                                                                                                                      
  Rails.logger.info "Authenticating user..."                                                                                                                                                                                                                                                                                                                                          
  Rails.logger.silence do                                                                                                                                                                                                                                                                                                                                                             
    @current_user ||= User.find_by(token: token)                                                                                                                                                                                                                                                                                                                    
  end
end

I couple this with adding the token to:

config/initializers/filter_parameter_logging.rb