delayed_job not logging

青春壹個敷衍的年華 提交于 2019-11-30 08:36:21

问题


I cannot log messages from my delayed_job process. Here is the job that is being run.

class MyJob
  def initialize(blahblah)
    @blahblah = blahblah
    @logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
  end
  def perform
    @logger.add Logger::INFO, "logging from delayed_job"
    #do stuff
  end
end

I've tried various logging levels, and I have config.log_level = :debug in my environment configuration. I run delayed_job from monit. I'm using delayed_job 3.0.1 with ruby 1.9.3 and rails 3.0.10.


回答1:


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.




回答2:


In RAILS_ROOT/config/initializers have a file called delayed_job_config.rb with these lines:

Delayed::Worker.logger = Rails.logger
Delayed::Worker.logger.auto_flushing = true

Remember to re-start your workers after doing this.

Let me know if this helps




回答3:


I don't see why you would set the logger in the job. When I've done this I set the worker to to use a specific file on start e.g. Logger.new("log/worker_#{worker_number}") which ensures that each worker outputs to it's own file and you don't have to worry about multiple workers writing to the same file at the same time (messy).

Also, in plain ol' ruby you can call @logger.info "logging from delayed_job".

Finally, i'm pretty sure that 'perform' is called directly by your worker and instantiated, so you can refactor to:

class MyJob
 def perform(blahblah)
  @logger.add Logger::INFO, "logging from delayed_job"
  @blahblah = blahblah
  #do stuff
 end
end



回答4:


This is working just fine for me in Rails 3.2:

class FiveMinuteAggregateJob < Struct.new(:link, :timestamp)
  def perform
    Rails.logger.info 'yup.'
  end
end


来源:https://stackoverflow.com/questions/9507765/delayed-job-not-logging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!