-->

Where should I define my resque logger in my rails

2019-06-22 04:49发布

问题:

I have set up Resque in my rails application and it's all working fine. The question I have is where should the logger setup go. Should it be in the initializer or in the rake task? It works when set up in both. The reason I ask is I have seen it used in both in examples across the net.

I am thinking that it should probably be in the initializer as it's best practice to put setup into initializers.

config/initializers/resque.rb

logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')
logfile.sync = true
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO

I am then writing out using the Resque.logger syntax in the rake task and jobs.

E.G:

Resque.logger.info "Resque task started!"

Many thanks in advance.

EDIT
I'll stick with the initializer then.

回答1:

I would definitely put it inside initializer, since it needs to be called only once, while setting up your server.



回答2:

Another choice: config the resque logger inside every job if config it in initializer(config/initializers/resquer.rb) or rake (lib/tasks/resque.rake) neither work for you, it is not perfect but just works.

class OneJob
  def self.perform
    Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
    Resque.logger.level = Logger::DEBUG   

    Resque.logger.info 'Job starts'
    ...
  end
en