Rails Console Is Not Outputting SQL Statements to My Development Log

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')

Development.log log file isn't logging Rails SQL queries

Apparently showing the SQL in the console rather than development.log is default behavior for Rails 3.1. I haven't found a configuration option for changing that behavior, but I have found that, once the console is running, you can just do:

irb(main):001:0> ActiveRecord::Base.logger = Rails.logger

and that will take the sql out of the console and put it back in development.log. Or, if you don't want to do that every time you fire up the console, you can edit gems/railties-(version)/lib/rails/console.rb, and make the above assignment after the line in the start method that reads:

@app.load_console

Not saying it's a good solution, but it'll tide me over until I find something better...

How to show SQL queries run in the Rails console?

Rails 3+

Enter this line in the console:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Rails 2

Enter this line in the console:

ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

In the rails 4 console, how do I log all sql activity to file BUT not to the console output?

This ActiveRecord::Railtie code is what says to broadcast logger messages to the console:

# When loading console, force ActiveRecord::Base to be loaded
# to avoid cross references when loading a constant for the
# first time. Also, make it output to STDERR.
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
require "active_record/base"
console = ActiveSupport::Logger.new(STDERR)
Rails.logger.extend ActiveSupport::Logger.broadcast console
end

So one thing you could do when you first get into the console is to set ActiveRecord::Base.logger to a new logger that does not broadcast its messages to the console. For example:

ActiveRecord::Base.logger = ActiveSupport::Logger.new("log/development.log")

"log/development.log" can be changed to wherever you want to send the SQL output.

Rails SQL insert statements not showing up in console

Put this in either in your .irbrc or .pryrc in your home directory

if defined?(Rails) && !Rails.env.nil?
puts '... ActiveRecord and ActiveResource Logger set to STDOUT'
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
ActiveResource::Base.logger = logger
end

You should start to see the sql statements executing in the rails console.
Hope this helps

Display SQL queries in log with Rails 4

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')

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


Related Topics



Leave a reply



Submit