本文介绍了Rails控制台未将SQL语句输出到我的开发日志中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我通过本地主机访问Webrick服务器时,或者当我运行Rails迁移时,正确写入了development.log.但是,当我使用"rails c"启动我的rails控制台,然后尝试创建一个新的数据库对象并通过"user.save"之类的命令将其保存时,我在控制台中看到了SQL语句,但是在开发日志.

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.

大多数人在回答类似这样的问题时都会说检查以确保配置设置为正确的环境".我已经做到了,并且可以说在我的系统上,这是为全新的Rails应用程序而发生的.

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!

推荐答案

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

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会将所有SQL语句记录到stdout,从而在终端中显示它们.并且由于Logger.new接受任何流作为第一个参数,因此您可以让它写入rails development.log:

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

这篇关于Rails控制台未将SQL语句输出到我的开发日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:09