Immediate log messages of resque workers

The following description and code example was tested with the following gem spec:

rails (3.2.11)
resque (1.24.1)
resque-scheduler (2.0.1)

Since the default Resque.logger does not immediately flush all log messages to the log file, we want to use our own logger instance and a separate log file. All we have to do are the following steps:

  1. If you do not have a resque.rake rake file then go ahead and create one in your lib/tasks folder.
  2. To initialize a new logger we will use the Resque.before_fork hook inside the setup task.
  3. Open a new log file in your preferred location and activate the synchronization flag to avoid block buffering.
  4. Create a new instance of ActiveResource::BufferedLogger and set it as the default Resque.logger.
  5. Set the appropriate log level for your needs.
  6. That’s it!

The complete code will be show below.

require "resque/tasks"
require 'resque_scheduler/tasks'

task "resque:setup" => :environment do
  Resque.before_fork = Proc.new { 
    ActiveRecord::Base.establish_connection

    # Open the new separate log file
    logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')

    # Activate file synchronization
    logfile.sync = true

    # Create a new buffered logger
    Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
    Resque.logger.level = Logger::INFO
    Resque.logger.info "Resque Logger Initialized!"
  }
end

One response to “Immediate log messages of resque workers”

  1. I am a newbe to resque and have been looking at logging. Having looked at your approach I have tried it and I get an error with this line when I queue a job:

    Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
    uninitialized constant ActiveSupport::BufferedLogger

    If I replace it with this line then it all works fine.

    Resque.logger = ActiveSupport::Logger.new(logfile)

    I had previously been using this in the initializer and think putting this into the rake task is a better solution.

    Many thanks for the post.

Leave a Reply

Your email address will not be published. Required fields are marked *