【问题标题】:How to access ActiveJob Logger at rescue_from block?如何在rescue_from 块访问ActiveJob Logger?
【发布时间】:2016-05-11 12:43:56
【问题描述】:

如果我在 perform() 中登录,我会得到一个漂亮的输出:

  def perform(*args)
    begin
      MyActivity.run(*args)
    rescue StandardError => e
      Rails.logger.debug e
      raise e
    end
  end

13:55:10 resque.1 | [ActiveJob] [MyJob] [18cd0f78-d784-48a7-a573-0b3aa95c51cc] Connection refused - connect(2) for "fe80::1%lo0" port 3000

如果我在rescue_from 块内登录,我不会得到ActiveJobJobId 的标签/标签

rescue_from(StandardError) do |e|
  Rails.logger.debug e
end

13:55:11 resque.1 | Connection refused - connect(2) for "fe80::1%lo0" port 3000

我认为在 rescue_from 块上,作业已经终止,所以我们不再有 JobId

有没有办法像perform 中的日志一样获取输出?

【问题讨论】:

    标签: ruby-on-rails logging resque rails-activejob


    【解决方案1】:

    刚刚发现我可以这样做:

      rescue_from(StandardError) do |e|
        Rails.logger.debug "[ActiveJob] [#{self.class}] [#{job_id}] #{e}"
      end
    

    更好的是,如果我不想为任何工作记录任何类型的错误,我可以在 application_job.rb 中编写这段代码。

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      相关资源
      最近更新 更多