title: Ruby on Rails writing ActiveJob sub-class logs to a desired file
body: <p><strong>Ruby : 2.3.2</strong></p>
  
  <p><strong>Rails: 5.0.0.1</strong></p>
  
  <p>I have following classes</p>
  
  <pre><code>class JobA &lt; ApplicationJob
    def perform(args)
      Rails.logger.debug "JobA log"
  
      if condition_1
        JobB.perform_later(args)
      else
        JobC.perform_later(args)
      end
    end
  end
  
  class JobB &lt; ApplicationJob
    def perform(args)
      Rails.logger.debug "JobB log"
    end
  end
  
  class JobC &lt; ApplicationJob
    def perform(args)
      Rails.logger.debug "JobC log"
    end
  end
  
  class RecurringJob
    def perform
      JobA.perform_later(args)
    end
  end
  </code></pre>
  
  <p>I desire to redirect logs written by each of JobA, JobB, JobC classes to a desired file. However it is not working.</p>
  
  <p>I tried following</p>
  
  <p><strong>/config/initializers/logging.rb</strong></p>
  
  <pre><code># Reference: https://stackoverflow.com/a/44168717/936494
  class SelectedJobsLogger
    LOG_FILE_PATH = Rails.root.join('log', 'selected_jobs.log')
    class &lt;&lt; self
      cattr_accessor :logger
      delegate :debug, :info, :warn, :error, :fatal, :to =&gt; :logger
    end
  end
  
  module SelectedJobsLogHelpers
    LOGGER_CLAZZ = SelectedJobsLogger
  
    private
  
    def log_debug_message(msg)
      LOGGER_CLAZZ.debug(msg)
    end
  end
  
  SelectedJobsLogger.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(logger_clazz::LOG_FILE_PATH))
  SelectedJobsLogger.logger.level = Rails.logger.level
  </code></pre>
  
  <p>And then updated my job classes in following manner</p>
  
  <pre><code>class JobA &lt; ApplicationJob
    include SelectedJobsLogHelpers
  
    def perform(args)
      log_debug_message "JobA log"
  
      if condition_1
        JobB.perform_later(args)
      else
        JobC.perform_later(args)
      end
    end
  end
  
  class JobB &lt; ApplicationJob
    include SelectedJobsLogHelpers
  
    def perform(args)
      log_debug_message "JobB log"
    end
  end
  
  class JobC &lt; ApplicationJob
    include SelectedJobsLogHelpers
  
    def perform(args)
      log_debug_message "JobC log"
    end
  end
  </code></pre>
  
  <p>With that in place only <code>"JobA log"</code> is getting written to <code>log/selected_jobs.log</code> and <code>JobB log</code> and <code>JobC log</code> are getting written to <code>log/development.log</code></p>
  
  <p><strong>log/selected_jobs.log</strong></p>
  
  <pre><code># Logfile created on 2019-07-11 20:55:47 +0530 by logger.rb/56438
  JobA log
  </code></pre>
  
  <p><strong>log/development.log</strong></p>
  
  <pre><code>[ActiveJob] Enqueued JobA (Job ID: cbdc5989-40a5-4dd9-8ade-b14ea85c0214) to Sidekiq(queue_1) with arguments: 63
  [ActiveJob] [JobA] [cbdc5989-40a5-4dd9-8ade-b14ea85c0214] Performing JobA from Sidekiq(my_queue) with arguments: 63
  [ActiveJob] [JobA] [cbdc5989-40a5-4dd9-8ade-b14ea85c0214] Enqueued JobB (Job ID: a1de8536-1cda-47b7-ba7e-7e3e913c58b3) to Sidekiq(queue_2) with arguments: 162
  [ActiveJob] [JobA] [cbdc5989-40a5-4dd9-8ade-b14ea85c0214] Performed JobA from Sidekiq(my_queue) in 160.3ms
  [ActiveJob] [JobB] [a1de8536-1cda-47b7-ba7e-7e3e913c58b3] Performing JobB from Sidekiq(queue_2) with arguments: 162
  [ActiveJob] [JobB] [a1de8536-1cda-47b7-ba7e-7e3e913c58b3] JobB log
  [ActiveJob] [JobB] [a1de8536-1cda-47b7-ba7e-7e3e913c58b3] Performed JobB from Sidekiq(queue_2) in 349.68ms
  
  [ActiveJob] Enqueued JobA (Job ID: 90b1863e-4967-41d6-8ad9-a9a329df46e9) to Sidekiq(queue_1) with arguments: 63
  [ActiveJob] [JobA] [90b1863e-4967-41d6-8ad9-a9a329df46e9] Performing JobA from Sidekiq(queue_1) with arguments: 63
  ....
  ...
  
  [ActiveJob] [JobA] [90b1863e-4967-41d6-8ad9-a9a329df46e9] Performed JobA from Sidekiq(queue_1) in 13.21ms
  </code></pre>
  
  <p>Also as can be seen in <strong>log/selected_jobs.log</strong> the job logs are not written in the format like they are getting written in format <strong>log/development.log</strong>. I need the logs in former file to be written in the same format as they are in latter. </p>
  
  <p>Can anybody please help me achieving the desired thing?</p>