非常基本的 ruby​​/sinatra/heroku/调试问题:如何查看 put 和 p 的输出?

发布于 2024-10-16 19:19:47 字数 395 浏览 4 评论 0原文

我正在尝试构建一个部署在 heroku 上的非常简单的 sinatra 应用程序。

我们的应用程序不会将内容输出到网络浏览器,而是通过 API 与另一台计算机进行通信。因此,我在使用应用程序时仅向浏览器打印一些额外的调试信息的常用技巧不起作用。

我在相关应用程序中看到的示例代码显示了使用多个“puts”或“p”语句来查看发生了什么...

输出去了哪里,我可以在程序执行时或之后看到该输出。

一般来说,如果你在 Heroku 托管的代码上乱搞,那只是没有做你想要的事情,那么在代码中的各个位置输出消息(如“foo equals 123”)的最简单方法是什么,这样你就可以看到输出到弄清楚代码中发生了什么?

p 和 put 不输出,因此当我输入“heroku log”时我可以看到日志......

I'm trying to build a very simple sinatra app deployed on heroku.

our app is not outputting stuff to a web browser, it's communicating with another computer via an API. so my usual trick of just printing a little extra debugging info to the browser while I'm using the app doesnt work.

the sample code I've seen for related apps show multiple 'puts' or 'p' statement used ot sort of see what's going on...

where does the output go that I can see that output as the program executes, or afterwards.

and in general, if you're flailing around with code hosted at Heroku that's just not doing what you want, what IS the easiest way to at various places in the code output messages like "foo equals 123" so you can see that output to figure out what's happening in the code?

p and puts dont output so the logs I can see when I type "heroku logs" for example...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不离久伴 2024-10-23 19:19:47

如果您使用 cedar 堆栈,请尝试在 config.ru 中添加一行:

$stdout.sync = true

http://devcenter .heroku.com/articles/ruby#logging

原始帖子于 2011 年 2 月发布,Cedar stack 于 5 月推出,因此这似乎对原始问题没有帮助,但有些人可能会发现这可以帮忙吧。
http://blog.heroku.com/archives/2011/5/31/青瓷_雪松/

If you use a cedar stack, try to put a line bellow in config.ru,

$stdout.sync = true

http://devcenter.heroku.com/articles/ruby#logging

Original post was in February 2011, and Cedar stack was introduced in May, so this doesn't seem to be help for original question, but some of you may find this could be help.
http://blog.heroku.com/archives/2011/5/31/celadon_cedar/

命比纸薄 2024-10-23 19:19:47

根据 http://docs.heroku.com/logging 你应该能够拥有 puts如果您添加基本记录器(显然已默认添加到 2011 年 2 月 2 日之后创建的所有应用程序中),p 只需转到您的日志即可。

对于使用 Sinatra 和 Logger 进行非 Heroku 基本日志到文件:

require 'logger'
Dir.mkdir('logs') unless File.exist?('logs')
$log = Logger.new('logs/output.log','weekly')

configure :production do
  $log.level = Logger::WARN
end
configure :development do
  $log.level = Logger::DEBUG
end

get "/" do
  $log.debug "Hello, World!"
end

According to http://docs.heroku.com/logging you should be able to have puts and p just go to your log if you add the basic logger (which has apparently been added by default to all apps created after February 2nd, 2011).

For non-Heroku basic log-to-file with Sinatra and Logger:

require 'logger'
Dir.mkdir('logs') unless File.exist?('logs')
$log = Logger.new('logs/output.log','weekly')

configure :production do
  $log.level = Logger::WARN
end
configure :development do
  $log.level = Logger::DEBUG
end

get "/" do
  $log.debug "Hello, World!"
end
沉默的熊 2024-10-23 19:19:47

这会工作得很好。 test_logging.rb

require 'sinatra'
require 'logger'

enable :logging

before do
  logger.level = Logger::DEBUG
end

get '/' do
  logger.debug "Handling 'hello world' request."
  logger.info "Hello world."

  return "<h1>Hello World</h1>"
end

This will work fine. test_logging.rb

require 'sinatra'
require 'logger'

enable :logging

before do
  logger.level = Logger::DEBUG
end

get '/' do
  logger.debug "Handling 'hello world' request."
  logger.info "Hello world."

  return "<h1>Hello World</h1>"
end
偏闹i 2024-10-23 19:19:47

有关如何写入记录器的提示,请参阅此处:http://mikenaberezny。 com/2007/02/24/rails-logging-tips/

给出的示例是:

class HomeController < ActionController::Base
  def index
    logger.info 'informational message'
  end
end

See here for tips on how to write to the Logger: http://mikenaberezny.com/2007/02/24/rails-logging-tips/

The example given is:

class HomeController < ActionController::Base
  def index
    logger.info 'informational message'
  end
end
看海 2024-10-23 19:19:47

这是对我有用的行:

# On config.ru, before run:
enable :logging, :dump_errors, :raise_errors

This is the line that works for me:

# On config.ru, before run:
enable :logging, :dump_errors, :raise_errors
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文