<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jack Wu</title>
    <description>The latest articles on DEV Community by Jack Wu (@xuwupeng2000).</description>
    <link>https://dev.to/xuwupeng2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F73438%2F68dad6fa-48af-414d-b0bb-4e125cd09e80.png</url>
      <title>DEV Community: Jack Wu</title>
      <link>https://dev.to/xuwupeng2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xuwupeng2000"/>
    <language>en</language>
    <item>
      <title>How to configure Rails, ActiveJob, Grape to use JSON log</title>
      <dc:creator>Jack Wu</dc:creator>
      <pubDate>Wed, 24 Aug 2022 09:06:18 +0000</pubDate>
      <link>https://dev.to/xuwupeng2000/how-to-configure-rails-activejob-grape-to-use-json-log-4ng</link>
      <guid>https://dev.to/xuwupeng2000/how-to-configure-rails-activejob-grape-to-use-json-log-4ng</guid>
      <description>&lt;p&gt;Rails, activejob, grape all JSON format logging&lt;/p&gt;

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

&lt;p&gt;In my case there are 4 logging sources&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rails app log&lt;/li&gt;
&lt;li&gt;Sidekiq log&lt;/li&gt;
&lt;li&gt;ActiveJob log&lt;/li&gt;
&lt;li&gt;Grape log&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rails log is the simple one, add this gem and configure it as below:&lt;br&gt;
gem "lograge"&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gHZGzgb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo3yjcqkn86tphvl5mhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gHZGzgb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo3yjcqkn86tphvl5mhh.png" alt="Image description" width="697" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sidekiq log and ActiveJob log are duplicate. In my case I only want to keep the ActiveJob log.&lt;br&gt;
BTW: Sidekiq log has a JSON fomatter， you can follow the link below to configure it： &lt;a href="https://github.com/mperham/sidekiq/wiki/Logging#output-format"&gt;https://github.com/mperham/sidekiq/wiki/Logging#output-format&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to format ActiveJob log in JSON format you need to add the gem logist and configure your application as below. &lt;/p&gt;

&lt;p&gt;gem 'logist'&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lo78q4Pq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2b9rh37muwmznu9jtbb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lo78q4Pq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2b9rh37muwmznu9jtbb.png" alt="Image description" width="880" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;gem 'grape_logging'&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F480P3vz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri6bi9xukgaypiyx14o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F480P3vz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri6bi9xukgaypiyx14o1.png" alt="Image description" width="619" height="467"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZOVI9j3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tagjm30t9ukrny2m1qyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZOVI9j3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tagjm30t9ukrny2m1qyw.png" alt="Image description" width="880" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, we take advantage of ActiveSupport instrument by which we connect grape log to Rails then we convert it into JSON format&lt;/p&gt;

&lt;p&gt;PS: &lt;br&gt;
There is another solution, if you don't need to handle grape and ActiveJob you can check this gem, it may take less effort. &lt;br&gt;
&lt;a href="https://github.com/reidmorrison/semantic_logger"&gt;https://github.com/reidmorrison/semantic_logger&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>activejob</category>
      <category>grape</category>
    </item>
  </channel>
</rss>
