How to filter or remove ActiveJob argument logging?

I am using ActiveJob Rails, and one of my assignments takes raw email as input. When debugging, this can lead to a huge amount of noise in my application log. How can i avoid this?

[ActiveJob] Enqueued EmailParserJob (Job ID: 9678f343-c876-4f9f-9cc7-db440634e178) to DelayedJob(default) with arguments: "NOISE" 
+8
ruby-on-rails activesupport rails-activejob
source share
4 answers

It seems the only way to override ActiveJob's internal logging method :

 class ActiveJob::Logging::LogSubscriber private def args_info(job) '' end end 

Put it somewhere in app/initializers/active_job_logger_patch.rb .

+4
source share

I used after_initialize to after_initialize in advance. It turned out that it works only in the perform_start method, but not enqueue .
Using the on_load method to select jobs. I believe the lazyload function in Rails causes the class to load after overriding.

 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 
+2
source share
+2
source share

One thing that may be useful here: In any case, a class that is subclassed from (Rails 5.1) ActiveJob :: Base (or any instance of the class called by a class subclassed from ActiveJob :: Base). The normal Rails.logger.info('log this') commands are about to enter the rails console (presumably via STDOUT).

I did not quite understand the mechanism that this Rails.logger grab triggers, but you can switch to ActiveJob :: Base.logger and use the knowledge about it: ( https://github.com/rails/rails/blob/b205ea2dc6c70b2b8e2134640e3056ed33fdc6be/activejob/ lib / active_job / logging.rb # L13 ) to change the behavior as you like.

So this allows you to register as you want:

1) Include require "active_job/logging" in your application.rb

2) In config / development.rb (or in some environments) you should include this line:

 config.active_job.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/#{Rails.env}.log")) 

3) Any logging inside ActiveJob subclasses, use this for logging:

 ActiveJob::Base.logger.info('(MyJob) Inside of a job but not going to STDOUT') 

If anyone can provide code explaining why Rails.logger.info behaves differently if inside the ActiveJob class, that would be a good read.

0
source share

All Articles