7

I want my delayed job "code" to log in a different log file for business requirements. So I log a custom status in a log called as dj.log. Inside the "serialized" job, I am putting the log statements to log in my file.

Here is how the setup is

Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 10
Delayed::Worker.delay_jobs = !( Rails.env.test? || Rails.env.development? ) #dont use delayed_job in development or test mode

#Delayed_job custom logger
DJ_LOGFILE = File.join(Rails.root, 'log', 'dj.log')

and here is the job that the workers actually do

    people.each {|p| Mailer.mail(1233, p).deliver; sent_to << p.email }                
    Logger.new(DJ_LOGFILE).info("[DELIVERED] All Emails delivered (#{sent_to.join(", ")})")

what could be the problem here ? please help

Anand
  • 10,310
  • 24
  • 90
  • 135

3 Answers3

13

DelayedJob maintains it's own logger so you'll need to point that to your specific log file. So, in your initializer file (either environment.rb or, better, a specific delayed_job.rb file in config/initializers) add the following:

Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'dj.log'))
Olly
  • 7,732
  • 10
  • 54
  • 63
  • my code works well in development and staging environments well! – Anand May 31 '11 at 11:24
  • it seems that any file pointer points to DelayedJob::Worker.logger (defualt Rails.logger) http://groups.google.com/group/delayed_job/browse_thread/thread/5c13d59aa4f5cb41 so.. the above code worked for me! – Anand May 31 '11 at 13:40
  • For better logging in production, use `Delayed::Worker.logger ||= Logger.new(File.join(Rails.root, 'log', 'delayed_job_production.log'), 10, 100*1024*1024)` to auto rotate the log files 100MB each, keeping 10 latest entries. – Alex Le Jan 19 '16 at 19:06
2

Here is my solution (tested with 2.1.4):

config/initializers/delayed_job.rb

Delayed::Worker.logger ||= Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
ExiRe
  • 4,727
  • 7
  • 47
  • 92
1

The problem is this bug: https://github.com/collectiveidea/delayed_job/issues/382 . . . . .

Joseph Shraibman
  • 462
  • 4
  • 11