Jack Wu
Posted on August 24, 2022
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
- Rails app log
- Sidekiq log
- ActiveJob log
- Grape log
Rails log is the simple one, add this gem and configure it as below:
gem "lograge"
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.
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:
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
Posted on August 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.