Logging in Delayed_Job

Logging in delayed_job?

When I needed log output from Delayed Jobs, I found this question to be fairly helpful.

In config/initializers/delayed_job.rb I add the line:

Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'dj.log'))

Then, in my job, I output to this log with:

Delayed::Worker.logger.info("Log Entry")

This results in the file log/dj.log being written to. This works in development, staging, and production environments.

delayed_job not logging

An explation could be that the job gets initialized only once on producer side. Then it gets serialized, delivered through the queue (database for example) and unserialized in the worker. But the initialize method is not being called in the worker process again. Only the perform method is called via send.

However you can reuse the workers logger to write to the log file:

class MyJob
def perform
say "performing like hell"
end

def say(text)
Delayed::Worker.logger.add(Logger::INFO, text)
end
end

Don't forget to restart the workers.

Delayed Job not logging in Production

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'))


Related Topics



Leave a reply



Submit