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
Related
How to see the live server log in production like it is shown in development?
My app is on digitalOcean, if that helps. I am using unicorn as my production server.
When I do tail -f log/production.log, I can just see some migration info like this, but not the live requests info along with the SQL queries being run.
Also in my production.rb, I changed config.log_level to :debug
Try adding these lines of code into config/environments/production.rb:
Rails.logger = Logger.new(STDOUT)
config.logger = ActiveSupport::Logger.new("log/#{Rails.env}.log")
Reference:
This article for more info on logger
I am using rails 4.2.6. And the log_level is :warn for production environment. But when I tail -f the production log file on server I am seeing that it logs at :info level. So the log file is getting bigger in a short time. I am really creazy. Is there any clue that how can I solve this issue?
I find the solution. The solution is this: config.action_view.logger = nil.
I managed to log into a custom log file using this configuration:
Delayed::Worker.logger =
ActiveSupport::BufferedLogger.new("log/#{Rails.env}_delayed_jobs.log", Rails.logger.level)
It's working locally. I can see the file change and all the logs I want.
But the problem is that when I deploy to Heroku and run the job, and try to read the contents of the file through the console, I don't get the expected results.
irb(main):070:0* File.read('/app/log/production_delayed_jobs.log')
=> "# Logfile created on 2013-06-20 08:36:43 +0000 by logger.rb/25413\n"
Note that I set the loggin level to :debug in production. In config/environments/production.rb I have this configuration:
config.log_level = :debug
Basically #yoav is right, you can't write to your own log files on heroku (no file system access). what you can do is described in the heroku logs right here: https://devcenter.heroku.com/articles/logging#writing-to-your-log
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.
I have some Rake tasks that produce CSV output which I'd like to redirect to a file and open with other tools, but when I run heroku rake foo > foo.csv I get log messages (SQL queries, etc.) in my output.
I've tried Rails.logger = Logger.new('/dev/null') and Rails.logger = Logger.new(STDERR) at the top of the Rake task and while those function as expected locally, they don't have any noticeable effect when I run the task on Heroku.
I'm not too shocked that Heroku would squash STDOUT and STDERR together but it's a mystery to me why sending to /dev/null would not kill the output.
Any help greatly appreciated.
Rails v3.0.0, Heroku bamboo-ree-1.8.7 stack, rake 0.9.2.
I was having the same problem, though I didn't run into it until I changed config/environments/production.rb to have this:
config.logger = Logger.new(STDOUT)
(I did this so that my app would log to the heroku log.)
My fix was this:
config.logger = Logger.new(STDOUT) unless 'rake' == File.basename($0)
From Heroku | Dev Center | Logging:
When a Rails app is pushed, we will automatically install the rails_log_stdout plugin into the application which will redirect logs to stdout.
I think Heroku includes (in the output sent through your git push command) a notification about this (and one other addition: for serving static/public content, if I remember correctly). You may only see the notifications for certain kinds of pushes though (complete slug rebuilds?). I remember seeing it when I recently pushed a new application to a Bamboo/MRI-1.9.2 stack, but I do not think I got the message every time I pushed changes to just the application’s code (maybe adding a new gem to the Gemfile is enough to trigger it?).
Several Rails subsystems keep their own logger binding (independent bindings whose values are often initialized from Rails.logger; reassigning the latter does not change the former):
ActiveRecord::Base.logger
ActionController::Base.logger
ActionMailer::Base.logger
Heroku’s changes probably set a new value for Rails.logger before ActiveRecord is initialized. When ActiveRecord is eventually loaded, it sets its own logger to be the same as Rails.logger (the Heroku/stdout one). When your task runs, it reassigns Rails.logger, but it is too late for this to have any effect on ActiveRecord::Base.logger (the only most likely to be handling the SQL logs).
You probably need to reassign some of these other logger bindings to squelch the logging going to STDOUT. Some other likely locations are listed in rails_log_stdout’s init.rb in the Rails 2 section.
I faced the same problem and found the following to be a more convenient workaround:
Add the following to config/environments/production.rb
config.logger.level = Logger.const_get(ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'INFO')
Push to Heroku, then when you run your rake tasks add LOG_LEVEL="fatal" to the end of the command (replace foo and foo.csv with your things):
heroku run rake foo LOG_LEVEL="fatal" > foo.csv
I have log_level set to fatal in the above example, but it can be any of the following: debug|info|warn|error|fatal. In our case, using the highest would mean nothing but the most fatal errors are outputted into the csv file.
Just to help anyone with a "fresh" Rails project pushing to Heroku:
You need a combination of #Matt Burke and #Hengjie's answer:
Add these two lines to config/environments/production.rb:
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get(ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'INFO')
This will setup a new STDOUT logger and allow you to easily control the log resolution with the LOG_LEVEL environment variable.
I solved this problem with the following change to production.rb:
if 'rake' == File.basename($0)
ActiveRecord::Base.logger = Logger.new('rake.log', 'daily')
end
I suppose we could ignore the output as well
if 'rake' == File.basename($0)
ActiveRecord::Base.logger = Logger.new('/dev/null')
end