DEV Community

Jack Wu
Jack Wu

Posted on

1

How to configure Rails, ActiveJob, Grape to use JSON log

Rails, activejob, grape all JSON format logging

Nowadays, most logging service eg: ES and SLS has better support for JSON format log.
In the Ruby + Rails eco system there was a number of solutions(gems) providing such ability but most of them are outdate.
In this article we will take a look of this topic and provide a working solution.

In my case there are 4 logging sources

  1. Rails app log
  2. Sidekiq log
  3. ActiveJob log
  4. Grape log

Rails log is the simple one, add this gem and configure it as below:
gem "lograge"
Image description

Sidekiq log and ActiveJob log are duplicate. In my case I only want to keep the ActiveJob log.
BTW: Sidekiq log has a JSON fomatter, you can follow the link below to configure it: https://github.com/mperham/sidekiq/wiki/Logging#output-format

In order to format ActiveJob log in JSON format you need to add the gem logist and configure your application as below.

gem 'logist'
Image description

Last, Let configure Grape log. My goal is to merge grape log and Rails log then log both of them in JSON. In order to do so you need to add another gem and configure it as below:

gem 'grape_logging'
Image description
Image description

As shown above, we take advantage of ActiveSupport instrument by which we connect grape log to Rails then we convert it into JSON format

PS:
There is another solution, if you don't need to handle grape and ActiveJob you can check this gem, it may take less effort.
https://github.com/reidmorrison/semantic_logger

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay