Stopped seeing SQL level logs on Heroku with RoR - ruby-on-rails

I've stopped seeing my app's SQL level logs. I only see the calls for the controllers.
Haven't changed any configuration.
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
production.rb
Has anyone experience the same with Heroku?

I had the same problem.
I added
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)
to produciton.rb and it started showing the SQL queries in the log again.

Related

error creating daily logs in rails app on heroku

I have a rails 5 app that I'd like to push to heroku, in which I have a logger that creates a daily log file, which I use to print out various pieces of status information. This works in development, but I get an error when I try to push it to heroku (after pushing everything to git).
The error I receive: "Errno::ENOENT: No such file or directory # rb_sysopen - /tmp/build_e3fe50d2e37e0a51f1bc7d94dd1fc2f3/log/daily_logs_production/2016-12-19.log
"
Here is the relevant portion from production.rb:
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new("#{Rails.root}/log/daily_logs_production/#{Time.now.strftime('%Y-%m-%d')}.log", 'daily')
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Any idea what is wrong? Thank you!
whether or not you are getting an error can be neglected.
due to the ephemeral filesystem of heroku dynos you should not use it for such purposes.
install a plugin of the Logging category instead.

Rails | How to see live server log in production?

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

Log issue in Rails4 with Docker running rake task

My rails4 application is running with docker. It runs a rake task for fetching messages from AWS SQS.
The problem I met is that logs can't show up in a console in time. The console doesn't show anything until exception/error comes. In other words, if my application works fine, no logs come to console. But if application went wrong, all the logs(info, warn and error) come together!
I already configure the config/production.rb as blow:
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get('INFO')
config.log_level = :info
I google 'rake task log was not working', but nothing useful. Is this a rails log problem or a rake task log problem, or maybe a docker problem?
Hoping that get some advice!
Try disabling output buffering to STDOUT. You can do this by adding this line to your rake task:
$stdout.sync = true
For Rails 4.x the log level is configuration
# Enable stdout logger
config.logger = Logger.new(STDOUT)
# Set log level
config.log_level = :ERROR
The logger level is set on the logger instance from config.log_level at:
(https://github.com/rails/rails/blob/v4.2.4/railties/lib/rails/application/bootstrap.rb#L70)

How to increase Heroku log drain verbosity to include all Rails app details?

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.

How do I keep log messages out of STDOUT when running Rake tasks on Heroku?

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

Resources