Rails 7.1 adds support for logging background job enqueue callers

Vishnu M

By Vishnu M

on April 18, 2023

This blog is part of our  Rails 7 series.

Rails 7.1 has introduced a new option in Active Job that allows us to add support for logging background job enqueue callers. It provides information about the location from where a job was enqueued, which can be immensely helpful during debugging. The following is an example of what the log output looks like:

1[ActiveJob] Enqueued NotifySubscribersJob (Job ID: 5945980f-303e-4c3f-af5b-e22be170f7c5) to Sidekiq(default)
2[ActiveJob] ↳ app/models/post.rb:14 in `notify_subscribers`

By examining the logs above, we can determine that the NotifySubscribersJob was enqueued from the notify_subscribers method in the Post model, providing us with a clear picture of the job's origin.

To enable this functionality, we need to set the following configuration in config/environments/development.rb.

1config.active_job.verbose_enqueue_logs = true

However, Rails 7.1 ships with this configuration set by default.

It's important to note that using verbose enqueue logs in production environment is not recommended, as it uses the Kernel#caller method to retrieve the execution stack, which can be slow.

Please check out this pull request for more details.