Fillype Farias
Fillype Farias

Reputation: 660

how to print text on heroku logs?

As far as I know, the best way to debugg in production with heroku is verifying it's logs. I want to display a variable as a string to this terminal but I couldn't yet with the configuration below.

from the command:

heroku logs

I want to display from a given page's controller something like:

puts "****test****"

this heroku article is saying that it needs to config with:

config.ru

$stdout.sync = true

and I write between these two lines:

require_relative 'config/environment'

$stdout.sync = true   # <----

run Rails.application

In the same article, is saying that we need to add config.logger = Logger.new(STDOUT) into "into your app’s configuration to get stdout logging".

So, added inside production.rb between these lines:

  # (...)

  config.logger = Logger.new(STDOUT) # <---

  config.log_level = :debug

  config.log_tags = [ :request_id ]

  # (...)

That doesn't work.

Upvotes: 1

Views: 1323

Answers (1)

Mark Merritt
Mark Merritt

Reputation: 2695

In your production.rb file you should have this configuration (it should already be there, but doesn't hurt to make sure)

if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = config.log_formatter
  config.logger    = ActiveSupport::TaggedLogging.new(logger)
end

This tells you that you need to make sure that the RAILS_LOG_TO_STDOUT environment variable has a value in production.

$ heroku config:set RAILS_LOG_TO_STDOUT=enabled

Then, in your controller, you can call something like

logger.debug "****test****"

Upvotes: 2

Related Questions