How to use Logger inside Resque worker in pure ruby ​​app

I have a ruby ​​( not Rails ) application using Resque. I would like to know what is going on with one of my workers.

For this I use the Logger class as stated in the official documentation .

The following describes how I would log inside the worker:

require 'resque'
require 'logger'

<code>

Resque.logger = Logger.new File.new('logfile.log', 'a')
Resque.logger.info "Whatever"

      

However, nothing is logged when starting my worker . It is as if the worker is actually ignoring all these log instructions. No error occurs. Other parts of the code actually work - only part of the log is ignored.

I tried using the log class itself (i.e. logger = Logger.new

) but the result is the same.

Do you have any idea on how I can register something inside my work frame?

Thank!

+3


source to share


3 answers


Use the following:



Logger.new(path_to_log_file).info(anything)

      

+1


source


You are missing the log level for the allowed logs of print information, according to the documentation you linked there, you can use something like this

Resque.logger.level = Logger::DEBUG

      

then if you look at the Logger docs it lists the levels as



DEBUG <INFO <WARN <ERROR <FATAL <UNKNOWN

so if you are using logger.info

you will need Logger::DEBUG

or Logger::INFO

to display the registered string.

Also make sure the log initialization is started by the process running the workers, not just the jobs of the queue process.

+1


source


my environment: rails 5.0.1, resque: 1.26.0.

I tried setting up the log in config/initializers/resque.rb

and out lib/tasks/resuqe.rake

, they don't work for me.

but set up rhesus recorder inside each job that works for me, although not perfect.

# update_xxx_job.rb
class UpdateXxxJob
  def self.perform
    Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
    Resque.logger.level = Logger::DEBUG   

    Resque.logger.info 'Job starts'
    ...
  end
end

      

more details in another answer .

0


source







All Articles