Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveJob + Sidekiq 6.0.3: How to write to a log file?

I'm using Sidekiq to get some background jobs done. I am also trying to log some messages from these jobs to a log file, but I have been unable to do this simple task.

Example of the kind of job I'm running

class TestJob < ApplicationJob
  queue_as :default

  def perform
    text = 'Print me to a file!'
    Rails.logger.error "Rails.logger.info : #{text}"
    logger.error "logger.info : #{text}"
  end
end

Running TestJob.perform_now, in a controller action for example, works as expected, printing the messages to the server terminal output, as well as to logs/development.log.

But running TestJob.perform_later doesn't print my messages. Anywhere. Not to the Sidekiq terminal, the server terminal, log files, nothing.

I tried redirecting the logs, as suggested in the Sidekiq Logging wiki. But the messages didn't get printed there either.

I feel like I might be missing something crucial.

like image 958
hananamar Avatar asked Dec 04 '19 12:12

hananamar


People also ask

Should I use Activejob Sidekiq?

If you build a web application you should minimize the amount of time spent responding to every user; a fast website means a happy user.

How Sidekiq works or how would you implement Sidekiq?

Sidekiq is an open-source framework that provides efficient background processing for Ruby applications. It uses Redis as an in-memory data structure to store all of its job and operational data. It's important to be aware that Sidekiq by default doesn't do scheduling, it only executes jobs.

What is Activejob when should we use it?

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.


1 Answers

Try to call Sidekiq.logger, as in:

    Sidekiq.logger.error "logger.info : #{text}"

Putting "logger.info" in the log of an error might be a little weird, but I was just taking from the example posted in the question.

like image 200
cdmo Avatar answered Oct 13 '22 01:10

cdmo