【问题标题】:Why can't I print ("puts") in heroku console?为什么我不能在 heroku 控制台中打印(“puts”)?
【发布时间】:2011-11-22 02:11:58
【问题描述】:

我有一个 Rails 应用程序部署到 heroku,需要在 heroku console 中处理一些数据。为了帮助我做到这一点,我在我的 lib 目录中创建了cmd.rb,其中包含一些方法。这是它的样子:

class Cmd
  def self.hello
    puts "Hello World"
  end

  def self.create_member
    # real code with multiple "puts" statements here
  end
end

这就是我尝试在 heroku 控制台中运行它时得到的结果:

$ heroku console
Ruby console for myapp.heroku.com
>> puts User.count
515
=> nil
>> 3.times {|i| puts i}
0
1
2
=> 3
>> require "#{Rails.root}/lib/cmd"
=> true
>> Cmd
=> Cmd
>> Cmd.hello
=> nil

上面明显缺少“Hello World”。现在,如果我在另一个终端窗口中执行此操作,我可以看到输出:

heroku logs --tail

但该输出中有很多噪音,我希望能够逐步看到事物。

【问题讨论】:

  • 我注意到,如果我使用heroku logs --tail --ps web.1,我得到的日志输出会少一些,尽管它仍然很吵。

标签: ruby-on-rails printing console heroku logging


【解决方案1】:

我认为这可能是因为 Heroku 实现控制台的方式:

在 Bamboo 上,heroku rake 和 heroku 控制台被实现为特殊情况的执行路径,输出通过 HTTP 传递。这种方法有许多弱点,例如超时和 rake 任务的非交互性。

因此,一种解决方案可能是升级到 Cedar:http://devcenter.heroku.com/articles/cedar

另一种解决方案是禁用rails_log_stdout 功能。我不知道该怎么做,但打开票可能会得到解决方案。

此外,您还可以过滤日志结果。请参阅“过滤”:http://devcenter.heroku.com/articles/logging

【讨论】:

    【解决方案2】:

    我认为您应该尝试使用 Rails.logger 而不是方法“puts”。

    【讨论】:

    • 我试过Rails.logger——同样的结果。 puts 基本上与 Rails.logger 去同一个地方。
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2018-04-15
    • 2022-06-16
    相关资源
    最近更新 更多