Enable immediate log messages of resque workers

, Andreas Katzian

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
Andreas Katzian
code rails resque worker logging BufferedLogger

comments powered by Disqus

 

Recent Posts

 

Enable immediate log messages of resque workers

Short code example how to to immediately log messages out of resque workers to a separate log file.

Kinect on MacOS X with Homebrew

How to get libfreenect running with homebrew on MacOS X 10.8

Updating sitemap file on Heroku

Updating the sitemap.xml on Heroku for your rails application by using the sitemap_generator gem and AWS S3/CloudFront.

Various ActiveResource Tips

Enabling Logging, Set different model name, Set HTTP headers, Set xml/json format