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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7534711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:20:49  来源:igfitidea点击:

Rails console is not outputting SQL Statements to my Development Log

rubyruby-on-rails-3loggingdevelopment-environment

提问by Inc1982

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.

当我通过本地主机访问我的 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!

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

回答by Marian Theisen

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.newaccepts any stream as first argument, you could just let it write to the rails development.log:

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

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

回答by Shanison

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/