Where should I define my resque logger in my rails application

968 views Asked by At

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.

2

There are 2 answers

0
Piotr Kruczek On BEST ANSWER

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

0
Spark.Bao On

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