I have a Rails app running on Heroku, it runs fine. I had it running with debug level logging, but now I switched back to INFO. What other levels of debugging are available? And when running for production, what level of logging should I use?
On Heroku you can set your log level by using the LOG_LEVEL environment variable
$ heroku config:set LOG_LEVEL=DEBUG
Valid values include DEBUG, INFO, WARN, ERROR, and FATAL. Alternatively you can set this value in your environment config:
config.log_level = :debug
If both are set LOG_LEVEL will take precedence. You'll most likely want INFO in production.
See the rails-stdout-logging gem
Logging on Heroku is just a stream of text lines. So you can log anything you want. You can also filter through that stream, use add-ons for enhanced search, and get a syslog drain of your logs.
Related
We have set logger as STDOUT in the rails configuration.
config.log_level = :info
config.logger = Logger.new(STDOUT)
We are expecting these logs in kubectl logs as well as datadog logs but STDOUT is not showing up there. We tried below code to test it.
def method_name
system('echo testing logging') - this shows up in kubectl/datadog logs
Rails.logger.info('STDOUT - testing logging') - this does not show up in kubectl/datadog log
end
We have changed the logger setting to output the logs to container file descriptor.
Reference: https://blog.eq8.eu/til/ruby-logs-and-puts-not-shown-in-docker-container-logs.html
Try to use the default config and make sure to set the environment variable RAILS_LOG_TO_STDOUT=true, for your deployment/replica set, and in production mode (RAILS_ENV=production). (In dev mode it always logs to console per default).
Actually, the official rails docker images used to have that set, but the newer recommended ruby docker images - of course - do not have Rails specific environment variables set.
(more: search for RAILS_LOG_TO_STDOUT in the release notes here, and see PR here)
When I tail the logs and hit my app, I have heroku[router] telling me I received a 500, and the app shows the usual "We're sorry, but something went wrong" message... but there are no additional details from Rails in the logs to tell me what is causing the 500.
I've read a couple of other threads that asked about this but they mostly end with people suggesting to do a rake:db migrate which fixed the error but there's no further discussion about the logging unfortunately. (And yes I'm sure there's no pending migrations 😄)
I have the rails_12factor gem installed
I've tried setting config.log_level = :debug in production.rb
I've tried creating a config variable of LOG_LEVEL set to DEBUG (which should supersede the config setting)
I've set $stdout.sync = true in config.ru as suggested here
What am I missing?
As an aside, have tried re-producing my actual problem locally with a backup of the database being used with Heroku but I can't reproduce it, so I'm finally forced to get to the bottom of this logging problem.
I first just got this to work by manually adding:
config.logger = Logger.new(STDOUT)
to production.rb but that obscured the real issue...
RACK_ENV and RAILS_ENV were both set to staging while I had the gem loading in the :production set... So although Heroku told me it was using the rails_12factor gem when the instance started, it wasn't really (I assumed it wouldn't be listed there if it wasn't a production environment, wrong assumption!)
staging.rb was loading production.rb which is why setting STDOUT there worked, thus confusing the issue further.
Thanks for the replies, and hope this helps someone else in future - go check your config variables and/or change where the gem is being loaded! :)
Debugging errors on heorku can get difficult most of the time.
For people facing a similar problem I suggest using PaperTrail heroku add-on for logging, It is free upto a certain limit.
It is realtime and provides with searching of logs, events hooks, mailing alerts etc. facilities. You will never have trouble debugging errors on heroku.
You can simply install it using the heroku cli command.
heroku addons:create papertrail
I've deployed to a VPS, but when visiting to my site, message shows:
We're sorry but something went wrong.
How could I start to debug?
I've deployed this commit successfully to heroku before. And before pushing to VPS, the site was running well.
I tried to see the log/production.log, but it only says Connecting to database specified by database.yml
Start with logs. Set more verbose log level to see what happens: set config.log_level = :debug in your config/environments/production.rb
More general, try something like errbit to see details about your production errors.
Presently I'm running a Rails 3.1.x app atop Heroku Celadon Cedar and it seems that log verbosity is very much lacking. I have set the log level to DEBUG a la heroku config:add LOG_LEVEL=DEBUG --app app_name, which matched up to their recommendation, however beyond that I cannot seem to pull in the log/* file contents.
Changing from Thin to Unicorn did increase verbosity slightly, but only in web worker requests. I still cannot pull down the db requests and so forth.
What is the best way to maximize log verbosity via the heroku "drain" mechanism so that one can pull all instance logs into one cohesive log?
(Ideally I'd like to include a method to dump this into one of my own log servers as this is just a pain the rear not being able to readily look at specific events and surrounding conditions in time.)
In staging.rb, production.rb or whatever environment you're running, you can insert the following:
STDOUT.sync = true
logger = Logger.new(STDOUT)
logger.level = 0 # Must be numeric here - 0 :debug, 1 :info, 2 :warn, 3 :error, and 4 :fatal
# NOTE: with 0 you're going to get all DB calls, etc.
Rails.logger = Rails.application.config.logger = logger
### NOTE: Be sure to comment out these:
# See everything in the log (default is :info)
# config.log_level = :debug # Commented out as per Chris' instructions from Heroku
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
I think the ActiveRecord logger object sends all SQL query logs with a DEBUG severity level. You may need to adjust the Rails.logger log level to get those in production too.
Since you are interested in dumping all Rails and Heroku logs to an external service, I can suggest you try the Progstr Logger free add-on. (Disclaimer, I work for Progstr). The add-on has a Ruby gem that will collect all Rails logs (everything that usually ends up in log/*). It will also set up a log drain that will fetch all system-level Heroku logs. In addition you get a cool web UI that lets you easily search for logs and some other nice features.
I am building an application with Heroku and having some problems.
I want to debug some content like I do with rails server:
logger.debug '...'
How can I do that in Heroku, so I can see the debugging in heroku logs? (or anything else..)
Thanks!
Details here: http://devcenter.heroku.com/articles/logging
Also make sure you set the right Logging level for your Rails app:
http://blog.sethladd.com/2005/11/adjust-log-level-in-ruby-on-rails.html
The Cedar stack in Heroku does not appear to be responsive to the LOG_LEVEL config (env) variable that works for previous stacks (I use the logging:expanded addon). I tried setting LOG_LEVEL to both debug and DEBUG without success.
Only by setting config.log_level = :debug in config/environments/production.rb am I able to see the output of 'logger.debug'.
Been fighting with this for a long time, solution is nice and simple:
Set in production.rb instead of
config.log_level = :debug
place:
config.logger = Logger.new(STDOUT)
config.logger.level = Logger::DEBUG
and you get the full logging output.
heroku logs on your command line will give you the logs for the current app. If you have expanded logging turned on you can tail this output
Using Rails 3.2, we made this configurable by modifying config/environments/{environment}.rb to drive it from an environment variable:
config.log_level = ENV["LOG_LEVEL"].to_sym if ENV["LOG_LEVEL"]
Then, we can modify the Heroku config variable to change it:
heroku config:set LOG_LEVEL=debug --app <app name>
This lets us easily log more or less as needed.
This worked for me:
heroku config:set LOG_LEVEL=debug
To write to your logs on Heroku, instead of using logger.debug "..." simply use puts:
puts "..."
You don't even need to set the config.log_level setting in config/environments/production.rb.
See the docs here.
config.log_level = ENV['APP_LOG_LEVEL'] ? ENV['APP_LOG_LEVEL'].to_sym : :error