Rails控制台未将SQL语句输出到我的开发日志

时间:2022-04-02 22:03:38

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.

当我通过localhost访问我的Webrick服务器时,或者当我运行rails迁移时,我的development.log被正确写入。但是,当我使用“rails c”启动我的rails控制台然后尝试创建一个新的数据库对象并通过像“user.save”这样的命令保存它时,我在控制台中看到了SQL语句,但没有写入开发日志。

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.

大多数人在回答类似这样的问题时说“请确保将配置设置为正确的环境”。我已经完成了这个,并且可以在我的系统上说这是一个全新的rails应用程序。

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

2 个解决方案

#1


77  

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

rails控制台永远不会写入日志文件,但您可以非常轻松地实现它,例如,如果您在启动rails控制台后执行以下操作

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:

rails会将所有SQL语句记录到stdout,从而在终端中显示它们。由于Logger.new接受任何流作为第一个参数,你可以让它写入rails development.log:

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

#2


34  

I am in Rails 2.3.8, the above answer doesn't really work for me.

我在Rails 2.3.8中,上面的答案并不适合我。

ActiveRecord::Base.logger = Logger.new STDOUT

The following actually works:

以下实际上有效:

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

Reference http://www.shanison.com/2012/03/05/show-sql-statements-in-rails-console/

参考http://www.shanison.com/2012/03/05/show-sql-statements-in-rails-console/

#1


77  

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

rails控制台永远不会写入日志文件,但您可以非常轻松地实现它,例如,如果您在启动rails控制台后执行以下操作

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:

rails会将所有SQL语句记录到stdout,从而在终端中显示它们。由于Logger.new接受任何流作为第一个参数,你可以让它写入rails development.log:

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

#2


34  

I am in Rails 2.3.8, the above answer doesn't really work for me.

我在Rails 2.3.8中,上面的答案并不适合我。

ActiveRecord::Base.logger = Logger.new STDOUT

The following actually works:

以下实际上有效:

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

Reference http://www.shanison.com/2012/03/05/show-sql-statements-in-rails-console/

参考http://www.shanison.com/2012/03/05/show-sql-statements-in-rails-console/