Where should I define my resque logger in my rails application

Deadly 提交于 2019-12-10 13:56:50

问题


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


来源:https://stackoverflow.com/questions/30776842/where-should-i-define-my-resque-logger-in-my-rails-application

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