ActiveJob logs as JSON

Alexandre Overtus
1 min readOct 2, 2022

--

Whenever we have to investigate a problem or monitor the behaviour of our app logs are a powerful tool but it can be overwhelming to produce/manage properly.

By default ActiveJob comes out of the box with text formatted logs

Performing HelloWorldJob (Job ID: ec2c75f1-e683-48bb-bf80-bab5a12d0262) from Sidekiq(default) enqueued at 2022-10-01T19:06:09Z with arguments: {:from=>"Belgium", :to=>"Canada"}Performed HelloWorldJob (Job ID: ec2c75f1-e683-48bb-bf80-bab5a12d0262) from Sidekiq(default) in 0.35ms

Those lines are produced by ActiveJob::LogSubscriber.

When an action is triggered, a job being enqueued or performed, ActiveJob emit an event thanks to ActiveSupport::Notifications.

We can write a new LogSubscriber which will output JSON formatted informations.

The last step is to unsubscribe the text version and subscribe the newly created JSON version.

# Disable text version
ActiveJob::LogSubscriber.detach_from :active_job
# Enable json version
ActiveJob::LogSubscriberJson.attach_to :active_job

The same job execution will output JSON formatted logs.

{"event":"active_job_performing","job_class":"HelloWorldJob","job_id":"78780a4b-1db0-446b-ba0b-31d0a78c7ff3","queue":"default","enqueued_at":"2022-10-01T19:18:28Z","arguments":[{"from":"Belgium","to":"Canada"}]}{"event":"active_job_performed","job_class":"HelloWorldJob","job_id":"78780a4b-1db0-446b-ba0b-31d0a78c7ff3","queue":"default","duration":2.22,"arguments":[{"from":"Belgium","to":"Canada"}]}

We can now perform complex queries against our log files, build indexes etc … without having to worry about doing any parsing operation.

Output example in AWS CloudWatch:

--

--