Search code examples
ruby-on-railsruby-on-rails-5pumarufus-scheduler

Pass rufus-scheduler logs to production.log file


How can I redirect all logs from rufus-scheduler to log/production.log?

For example, in console I can see something like that:

↳ config/initializers/XXX_scheduler.rb:15
{ 70173962355140 rufus-scheduler intercepted an error:
  70173962355140   job:
  70173962355140     Rufus::Scheduler::CronJob "43 12 * * *" {}
  70173962355140   error:
  70173962355140     70173962355140
  70173962355140     NoMethodError
  70173962355140     undefined method passenger_user for nil:NilClass
...

I found this error text in puma error (puma.error.log) file on production. Part of deploy.rb:

set :puma_error_log, "#{release_path}/log/puma.error.log"

How can I redirect all logs related to rufus-scheduler to production.log?


Solution

  • Sorry for the late answer.

    What you are seeing is the output of Scheduler#on_error. By default it outputs to stderr.

    This destination can be overriden as explained in the readme but it's probably better to override #on_error, as detailed in the readme as well.

    Your #on_error might thus look like

    def scheduler.on_error(job, error)
    
      Rails.logger.error(
        "err#{error.object_id} rufus-scheduler intercepted #{error.inspect}" +
        " in job #{job.inspect}")
      error.backtrace.each_with_index do |line, i|
        Rails.logger.error(
          "err#{error.object_id} #{i}: #{line}")
      end
    end
    

    and thus simply piggyback Rails logging infrastructure.

    The original #on_error is quite detailed. Feel free to take inspiration from it if you need more debugging information.