I'm using Rails' ActiveJob, and one of my jobs take a raw email as input. When debugging, this can result in a huge amount of noise in my application log. How can I avoid that?
我正在使用Rails的ActiveJob,我的一个工作是将原始电子邮件作为输入。调试时,这可能会导致应用程序日志中出现大量噪音。我怎么能避免这种情况?
[ActiveJob] Enqueued EmailParserJob (Job ID: 9678f343-c876-4f9f-9cc7-db440634e178) to DelayedJob(default) with arguments: "NOISE"
4 个解决方案
#1
4
It seems the only way is to override ActiveJob's internal logging method:
似乎唯一的方法是覆盖ActiveJob的内部日志记录方法:
class ActiveJob::Logging::LogSubscriber
private def args_info(job)
''
end
end
Put it somewhere into app/initializers/active_job_logger_patch.rb
.
把它放在app / initializers / active_job_logger_patch.rb中。
#2
3
See https://github.com/rails/rails/blob/4-2-stable/activejob/lib/active_job/logging.rb#L10
ActiveJob::Base.logger = Logger.new(nil)
#3
2
I used after_initialize
to hook beforehand. It turned out to work only in perform_start
method but not enqueue
.
Using on_load
method to hook works. I think it's the lazyload feature in Rails causing the class to be loaded after the override.
我事先使用了after_initialize来挂钩。事实证明它只在perform_start方法中工作,但没有入队。使用on_load方法挂钩工作。我认为这是Rails中的lazyload功能导致在覆盖后加载类。
ActiveSupport.on_load :active_job do
class ActiveJob::Logging::LogSubscriber
private def args_info(job)
# override this method to filter arguments shown in app log
end
end
end
#4
1
One thing that may be useful to note here: In any instance of a class that is subclassed from (Rails 5.1) ActiveJob::Base (or, any class instance called by a class subclassed from ActiveJob::Base) The normal Rails.logger.info('log this')
commands are going to get logged to the rails console (presumably via STDOUT).
这里可能有用的一件事:在从(Rails 5.1)ActiveJob :: Base(或者从ActiveJob :: Base子类调用的类调用的任何类实例)的子类的任何实例中,正常的Rails.logger .info('log this')命令将被记录到rails控制台(可能是通过STDOUT)。
I haven't quite figured out the mechanism that causes this hijacking of Rails.logger, but you can switch to ActiveJob::Base.logger and use the knowledge of this: (https://github.com/rails/rails/blob/b205ea2dc6c70b2b8e2134640e3056ed33fdc6be/activejob/lib/active_job/logging.rb#L13) to change the behavior as you wish.
我还没有弄清楚导致这种劫持Rails.logger的机制,但你可以切换到ActiveJob :: Base.logger并使用这方面的知识:(https://github.com/rails/rails/blob /b205ea2dc6c70b2b8e2134640e3056ed33fdc6be/activejob/lib/active_job/logging.rb#L13)根据需要更改行为。
So, this allows you to log as you want:
因此,这允许您根据需要进行记录:
1) Include require "active_job/logging"
in your application.rb
1)在application.rb中包含require“active_job / logging”
2) In config/development.rb (or whatever environments you want) include this line:
2)在config / development.rb(或您想要的任何环境)中包含以下行:
config.active_job.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/#{Rails.env}.log"))
3) Any logging inside of subclasses of ActiveJob, use this for logging:
3)在ActiveJob的子类中进行任何记录,使用它进行日志记录:
ActiveJob::Base.logger.info('(MyJob) Inside of a job but not going to STDOUT')
If anyone can point out the code that explains why Rails.logger.info
behaves differently when inside of an ActiveJob class that would be some good reading.
如果有人能指出解释为什么Rails.logger.info在ActiveJob类内部表现不同的代码,这将是一个很好的阅读。
#1
4
It seems the only way is to override ActiveJob's internal logging method:
似乎唯一的方法是覆盖ActiveJob的内部日志记录方法:
class ActiveJob::Logging::LogSubscriber
private def args_info(job)
''
end
end
Put it somewhere into app/initializers/active_job_logger_patch.rb
.
把它放在app / initializers / active_job_logger_patch.rb中。
#2
3
See https://github.com/rails/rails/blob/4-2-stable/activejob/lib/active_job/logging.rb#L10
ActiveJob::Base.logger = Logger.new(nil)
#3
2
I used after_initialize
to hook beforehand. It turned out to work only in perform_start
method but not enqueue
.
Using on_load
method to hook works. I think it's the lazyload feature in Rails causing the class to be loaded after the override.
我事先使用了after_initialize来挂钩。事实证明它只在perform_start方法中工作,但没有入队。使用on_load方法挂钩工作。我认为这是Rails中的lazyload功能导致在覆盖后加载类。
ActiveSupport.on_load :active_job do
class ActiveJob::Logging::LogSubscriber
private def args_info(job)
# override this method to filter arguments shown in app log
end
end
end
#4
1
One thing that may be useful to note here: In any instance of a class that is subclassed from (Rails 5.1) ActiveJob::Base (or, any class instance called by a class subclassed from ActiveJob::Base) The normal Rails.logger.info('log this')
commands are going to get logged to the rails console (presumably via STDOUT).
这里可能有用的一件事:在从(Rails 5.1)ActiveJob :: Base(或者从ActiveJob :: Base子类调用的类调用的任何类实例)的子类的任何实例中,正常的Rails.logger .info('log this')命令将被记录到rails控制台(可能是通过STDOUT)。
I haven't quite figured out the mechanism that causes this hijacking of Rails.logger, but you can switch to ActiveJob::Base.logger and use the knowledge of this: (https://github.com/rails/rails/blob/b205ea2dc6c70b2b8e2134640e3056ed33fdc6be/activejob/lib/active_job/logging.rb#L13) to change the behavior as you wish.
我还没有弄清楚导致这种劫持Rails.logger的机制,但你可以切换到ActiveJob :: Base.logger并使用这方面的知识:(https://github.com/rails/rails/blob /b205ea2dc6c70b2b8e2134640e3056ed33fdc6be/activejob/lib/active_job/logging.rb#L13)根据需要更改行为。
So, this allows you to log as you want:
因此,这允许您根据需要进行记录:
1) Include require "active_job/logging"
in your application.rb
1)在application.rb中包含require“active_job / logging”
2) In config/development.rb (or whatever environments you want) include this line:
2)在config / development.rb(或您想要的任何环境)中包含以下行:
config.active_job.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/#{Rails.env}.log"))
3) Any logging inside of subclasses of ActiveJob, use this for logging:
3)在ActiveJob的子类中进行任何记录,使用它进行日志记录:
ActiveJob::Base.logger.info('(MyJob) Inside of a job but not going to STDOUT')
If anyone can point out the code that explains why Rails.logger.info
behaves differently when inside of an ActiveJob class that would be some good reading.
如果有人能指出解释为什么Rails.logger.info在ActiveJob类内部表现不同的代码,这将是一个很好的阅读。