Log issue in Rails4 with Docker running rake task - ruby-on-rails

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)

Related

Duplicating Logger output to production.log and console for Rake tasks

I develop Rails application.
Currently, the default configuration of the logger instance (Rails.logger) is used, so it outputs the logging information to log/production.log in production environment.
Now, for some Rake tasks, I want to duplicate its output to console (terminal). I know that I can do things like
config.logger = Logger.new(STDOUT)
to switch the log destination from log/production.log to the terminal, but it does not duplicate the output so it is not the solution I'm looking for.
The motivation for achieving this configuration is to make the Rake task as cli to be more verbose on the terminal, and at the same time achieve all the application logs to be saved in log/production.log.
Question
How can I just duplicate the Rails.logger output to terminal?
By reading the article shared by #jamesc, I came up with following configuration, which seems to be doing what I wanted.
Rails.logger.extend(
ActiveSupport::Logger.broadcast(
ActiveSupport::Logger.new(STDOUT)
)
)

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

Delayed Jobs isn't logging on Heroku

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

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