DEV Community

Jack Wu
Jack Wu

Posted on

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

Top comments (0)