Rails console is not outputting SQL Statements to my Development Log

Inc1982 picture Inc1982 · Sep 23, 2011 · Viewed 23.9k times · Source

When I access my Webrick server via the localhost, or when I run rails migrations, my development.log is correctly written to. However, when I bootup my rails console with "rails c" and then try and create a new database object and save it via a command like "user.save", I see SQL statements in the console, but nothing is written to in the development log.

Most people when answering a question similar to this say "check to make sure that the config is set to the correct environment". I've done this, and can say on my system this happens for a completely new rails app.

Any help would be appreciated. Thanks!

Answer

Marian Theisen picture Marian Theisen · Sep 23, 2011

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