Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

very basic ruby/sinatra/heroku/debugging question: how to see output of puts and p?

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...

like image 982
jpw Avatar asked Feb 08 '11 23:02

jpw


3 Answers

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/

like image 137
hiroshi Avatar answered Sep 19 '22 10:09

hiroshi


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
like image 4
Phrogz Avatar answered Sep 22 '22 10:09

Phrogz


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
like image 3
Dave Sag Avatar answered Sep 22 '22 10:09

Dave Sag