Purplejacket
Purplejacket

Reputation: 2148

Why can't I print ("puts") in heroku console?

I have a Rails app deployed to heroku and need to munge some data in heroku console. To help me do this I've created cmd.rb in my lib directory with some methods in it. Here's what it looks like:

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

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

And here's what I get when I try to run this in heroku console:

$ 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

There's a notable lack of "Hello World" in the above. Now I can see the output if I do this in another terminal window:

heroku logs --tail

But there's a lot of noise in that output, and I'd like to be able to see things incrementally.

Upvotes: 3

Views: 2439

Answers (2)

B Seven
B Seven

Reputation: 45941

I think it may be because of the way Heroku implemented the console:

On Bamboo, heroku rake and heroku console were implemented as special-cased execution paths, with output delivered over HTTP. This approach has a number of weaknesses, such as timeouts and non-interactivity on rake tasks.

So, one solution might be to upgrade to Cedar: http://devcenter.heroku.com/articles/cedar

Another solution could be to disable the rails_log_stdout functionality. I didn't figure out how to do that, but opening a ticket may get a solution.

Also, you may be able to filter the log results. See 'Filtering': http://devcenter.heroku.com/articles/logging

Upvotes: 1

K'ao
K'ao

Reputation: 330

I think you should try to use Rails.logger instead of the method 'puts'.

Upvotes: 0

Related Questions