Rails 3.2 silence logging deprecation warning - ruby-on-rails

In Rails 3.2 I get deprecation warning, when using logger.silence {}. In release note:
"ActiveSupport::BufferedLogger#silence is deprecated. If you want to squelch logs for a certain block, change the log level for that block."
How I can easily change log level for the block?

It appears that logger.silence is being replaced by simply silence:
logger.silence do
#your silenced code here
end
becomes:
silence do
#your silenced code here
end
At least it doesn't generate the depreciation warning anymore, and it does silence the logged output.

The first answer is good, but not complete. We were having issues trying to figure this one out too. silence &block has been deprecated in Rails 3, so you should use the updated syntax calling the logger directly:
Rails.logger.silence do
# your code here...
end
For even more sweet, sweet customizability, you can pass a log level to #silence().

def silent_method
old_level = Rails.logger.level
Rails.logger.level = 7
result = your_code_here
Rails.logger.level = old_level
result
end

Or quietly{}, since silence requires a stream.

Related

Rails 5 upgrade Halt callback config issue

I have recently upgraded from Rails 4.2 to 5.0. I know about the change in callback halting using throw(:abort) instead of returning false. My problem is that I can't make the deprecation warnings go away.
DEPRECATION WARNING: Returning false in Active Record and Active Model callbacks will not implicitly halt a callback chain in Rails 5.1. To explicitly halt the callback chain, please use throw :abort instead.
I have made config/initializers/callback_terminator.rb file with following code
ActiveSupport.halt_callback_chains_on_return_false = false
but I am not still not able to get rid of the warning. Nor am I getting the expected behaviour. It seems that this configuration is not being applied.
Is there something I am missing?
Put the config in after config.after_initialize block in application.rb file like this.
config.after_initialize do
ActiveSupport.halt_callback_chains_on_return_false = false
end

Treat warnings as failures

I'm trying to clean up my test suite and to make this easier I'd like to have rspec raise an error when ever it encounters a warning. Rather then pass the test and carry on, I'd like the test to fail. Is there a way I can configure rspec to do this?
Are you talking about deprecation warnings? Or warnings in general?
I know you can raise errors when you hit deprecation warnings by setting config.active_support.deprecation = :raise in your test.rb
Per the RubyDoc for RSpec on Method: RSpec::Matchers#output, you should be able to fail the test by using expect(method).to_not output.to_stdout. I'm no expert in using RSpec, but in theory that should work. RSpec-Puppet has a very similar functionality built in that fails on warnings by using Puppet.expects(:warning).never .
I'm not sure this will work everywhere, since there is no contract that warnings have to be issued through Logger#warn, but this might do a decent job.
class Logger
def warn(*args)
super
if Rails.env.test?
raise(RuntimeError, "failing upon warn")
end
end
end
log = Logger.new
log.warn("foo")
Rspec gives us a simple way to do this globally:
config.before do
expect_any_instance_of(Logger).to_not receive(:warn)
end

How do I turn off Rails SQL logging in test?

For some reason (probably an updated gem) Rails is logging all my SQL commands now. I run autotest and they are being spammed during tests also. How do I turn it off?
I tried add this to config/environments/test.rb but it didn't work. logger was already nil.
# ActiveRecord::Base.logger = nil
# ActiveRecord::Base.logger.level = 1
Rails 4.0.0
Ok I found it. This worked:
config.after_initialize do
ActiveRecord::Base.logger = nil
end
Another thing you can do is call this code at runtime, it doesn't need to be in a config file.
For example, if you put in your specific test case
# test/functionals/application_controller_test.rb for example
ActiveRecord::Base.logger = nil
It would work just as well, and this way you can toggle it at runtime. Useful if you only want to stifle a few lines of code or a block.
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

Rails Rspec warning: "This dynamic method is deprecated."

My Rails 4/Ruby 2 app is throwing the following warning every time my RSpec tests create a FactoryGirl object: "DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead."
This warning is not thrown when I run my app in development. Is FactoryGirl's code throwing this? I tried to find some information but it doesn't look like other people are getting this.
If you tell Rails to give you a full stack trace for the deprecation warning, you should be able to diagnose it pretty easily. The warnings are coming from a library called ActiveSupport::Deprecation - tell it to run in debug mode.
# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
For me, the warnings were caused by an old version of the Stringex library.
FactoryGirl would make a new model, which would trigger a call to one of the Stringex methods, which would raise the warning, although there was no way to see that until I turned on full stack traces. bundle update stringex solved the issue with no problem.
It looks like it's coming from ActiveRecord.
module DeprecationWarning
def body
"#{deprecation_warning}\n#{super}"
end
def deprecation_warning
%{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")}
end
end
I'm not sure why you're not getting the warnings in development. Is your environment suppressing the warnings?

How to enable Ruby warnings in Rails?

I did this in test.rb:
def some_method
p "First definition"
end
def some_method
p "Second definition"
end
some_method
When I call ruby test.rb, it prints Second definition (expected)
When I call ruby -w test.rb, it prints Second definition (expected) and prints a warning test.rb:5: warning: method redefined; discarding old some_method
Is there a way to enable those warnings in Rails? (and print the warning to the console/log file)
Why I would like to enable warnings: For example if I inadvertently re-define a method in a controller, then I would be aware of the problem by looking at the warning printed to the console/log file. See here for an example.
Put this somewhere in your initialisation code (such as config/application.rb):
$VERBOSE = true
You'll probably also get some warnings from Rails itself though.

Resources