Rails: how to write to a user log file due to a rake task in production mode?

I am trying to write to my log files during a rake task. It works fine in development mode, but as soon as I switch to the production environment, nothing is written to the log files.

I read here

How to use my own log for my rake tasks in Ruby on Rails?

that this is normal behavior, and also found the #wontfix ticket in the lighthouse.

My question is: is there a way to deduce what happens when my rake task is executed? It performs some scans and works for several hours. I would prefer the output to go to a specific log file, e.g. /log/crawler.log

Now I just use this command to write to the log files:

ActiveRecord::Base.logger.info "Log Text" 

Thanks!

+6
ruby-on-rails logging web-crawler rake
source share
3 answers

The problem you are facing is that rails ignore information level logs in production mode.

I would recommend reading this: http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html

and create your own log:

 logger = Logger.new('logfile.log') logger.info "Something happened" 
+13
source share

You can create a new log with Logger.new ("file.log"), and then call its methods as follows.

  task :import_stuff => :environment do require 'csv' l = Logger.new("stuff.log") csv_file = "#{RAILS_ROOT}/stuff.csv" CSV.open(csv_file, 'r') do |row| l.info row[1] end end 
+2
source share

Perhaps you need to write out the buffer in which you need it:

 logger.flush 

or you can enable automatic flushing:

 task :foo => :environment do Rails.logger.auto_flushing = 1 Rails.logger.info "bar" end 
+2
source share

All Articles