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
Is There a Way in Ruby/Rails to Execute Code That Is in a String
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
In Ruby, What Is the Cleanest Way of Obtaining the Index of the Largest Value in an Array
Where to Place/Access Config File in Gem
Relative Path to Your Project Directory
How to Call Expire_Fragment from Rails Observer/Model
Why Should I Care About Rvm's Gemset Feature When I Use Bundler
Ruby: How to Concatenate Array of Arrays into One
How to Set Ruby's Load Path Externally
Why Do I Need to Work Harder to Make My Rails Application Fit into a Restful Architecture
Using Htaccess Password Protection on Rails
Why 'Self' Method of Module Cannot Become a Singleton Method of Class
"Uninitialized Constant" Error When Including a Module
How to Debug Http of Ruby Google-Api-Client
How to Input Integer Value to an Array, Based Preceeding Row + Column Values