Turn Off Verbose SQL/Activerecord for Rails 3.1.1

Disable Rails SQL logging in console

To turn it off:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

To turn it back on:

ActiveRecord::Base.logger = old_logger

How do I turn on SQL debug logging for ActiveRecord in RSpec tests?

By default, all your db queries will be logged already in test mode. They'll be in log/test.log.

How can I disable logging of asset pipeline (sprockets) messages in Ruby on Rails 3.1?

Place the following code in config/initializers/quiet_assets.rb

if Rails.env.development?
Rails.application.assets.try(:logger=, Logger.new('/dev/null'))
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
call_without_quiet_assets(env)
ensure
Rails.logger.level = previous_level
end
alias_method_chain :call, :quiet_assets
end
end

Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes before_dispatch, and now we're going for the root rack call instead)

Update: A proper Rack middleware solution (instead of fragile alias_method_chain) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735

rails with cucumber show unnecessary output

I was browsing around stack overflow, and today I
found a solution here -> https://stackoverflow.com/a/33987617/4394500

Basically, add

Rails.application.configure do
# ...
config.log_level = :info
end

to config/environments/test.rb

Rails console is not outputting SQL Statements to my Development Log

the rails console never writes to the log file, but you can achieve it quite easily, for example, if you execute following after starting the rails console

ActiveRecord::Base.logger = Logger.new STDOUT

rails will log all SQL statements to stdout, thus display them in your terminal.
and since Logger.new accepts any stream as first argument, you could just let it write to the rails development.log:

ActiveRecord::Base.logger = Logger.new File.open('log/development.log', 'a')


Related Topics



Leave a reply



Submit