joshuacronemeyer
joshuacronemeyer

Reputation: 1336

Heroku logging not working

I've got a rails 3.1 app deployed on Heroku Cedar. I'm having a problem with the logging. The default rails logs are working just fine, but when I do something like:

logger.info "log this message"

In my controller, Heroku doesn't log anything. When I deploy my app I see the heroku message "Injecting rails_log_stdout" so I think calling the logger should work just fine. Puts statements end up in my logs. I've also tried other log levels like logger.error. Nothing works. Has anyone else seen this?

Upvotes: 15

Views: 10984

Answers (4)

finiteautomata
finiteautomata

Reputation: 3813

The problem, as @MBHNYC correctly addressed, is that your logs are not going to STDOUT.

Instead of configuring manually all that stuff, Heroku provides a gem that does this all for you.

Just put

gem 'rails_12factor', group: :production

in your Gemfile, bundle, and that's it!

NOTE: This works both for Rails 3 and 4.

Source: Rails 4 on Heroku

Upvotes: 4

Phil Calvin
Phil Calvin

Reputation: 5105

MBHNYC's answer works great, but it makes it difficult to change the log level in different environments without changing the code. You can use this code in your environments/production.rb to honor an environment variable as well as have a reasonable default:

# https://github.com/ryanb/cancan/issues/511
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get((ENV["LOG_LEVEL"] || "INFO").upcase)

Use this command to set a different log level:

heroku config:set LOG_LEVEL=error

Upvotes: 35

Matt Sanders
Matt Sanders

Reputation: 10725

As of the moment, it looks like heroku injects these two plugins when building the slug:

rails_log_stdout - https://github.com/ddollar/rails_log_stdout

rails3_server_static_assets - https://github.com/pedro/rails3_serve_static_assets

Anything sent to the pre-existing Rails logger will be discarded and will not be visible in logs. Just adding this for completeness for anyone else who ends up here.

Upvotes: 4

MBHNYC
MBHNYC

Reputation: 1258

I was just having the same issue, solved by using the technique here:

https://github.com/ryanb/cancan/issues/511

Basically, you need to specify the logger outputs to STDOUT, some gems interfere with the logger and seem to hijack the functionality (cancan in my case).

For the click lazy, just put this in environments/production.rb

config.logger = Logger.new(STDOUT) 
config.log_level = :info

Upvotes: 20

Related Questions