<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maksym Ratoshniuk on Medium]]></title>
        <description><![CDATA[Stories by Maksym Ratoshniuk on Medium]]></description>
        <link>https://medium.com/@rtwnk?source=rss-f8ffed390905------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*YUBrtzCoplZZ8ML40_3xXw.jpeg</url>
            <title>Stories by Maksym Ratoshniuk on Medium</title>
            <link>https://medium.com/@rtwnk?source=rss-f8ffed390905------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 19:44:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rtwnk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[LogStage: zero-cost structured logging in Scala. Part 2: Practical example]]></title>
            <link>https://medium.com/@rtwnk/logstage-zero-cost-structured-logging-in-scala-part-2-practical-example-3ef27e67e7ee?source=rss-f8ffed390905------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ef27e67e7ee</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[logging]]></category>
            <dc:creator><![CDATA[Maksym Ratoshniuk]]></dc:creator>
            <pubDate>Sun, 21 Jul 2019 14:32:14 GMT</pubDate>
            <atom:updated>2019-07-21T14:32:14.103Z</atom:updated>
            <content:encoded><![CDATA[<p>This article is completely about how to use <a href="https://izumi.7mind.io/latest/release/doc/logstage/index.html"><strong>logstage</strong></a> and how it could help you while development own software. If you haven’t read <a href="https://medium.com/@rtwnk/logstage-zero-cost-structured-logging-in-scala-part-1-overview-d39e56d4acf2"><strong><em>theory overview</em></strong></a>, please, read before start this one.</p><h3>Agenda</h3><p>We we will implement custom sink for out log entries and make some queries to fetch analytics from out logs.</p><p>So, imagine, that you need to flush log messages to ware house like Redshift and using some UI like Grafana, make some queries to fetch metrics or analyze some vulnerable parts of your API. To simplify working environment, let’s use PostgreSQL as database. So, please prepare:</p><ul><li><strong>scala 2.12</strong>;</li><li><strong>postgres</strong> (docker or whatever you want);</li><li><a href="https://eggerapps.at/postico/"><strong>postico</strong></a> (GUI for your database, if you prefer).</li></ul><p>We use ZIO as the main framework for effect management. But it doesn’t matter right now. You can use whatever you want. As an API for postgres — doobie. These libraries doesn’t matter at all, the goal is to demonstrate customisations of logger.</p><h3><strong>Article structure</strong></h3><p>We will integrate logstage into real app step by step. Each step has own branch inside <a href="https://github.com/7mind/logstage-sample">Github project</a>, so don’t worry if you missed out something.</p><h3>Domain overview</h3><p>Imagine, that we need to write some platform that pulls data from multiple sources, process it and stores in S3 (routine, eh?)</p><p>And we would like to add some logs for our pipelines to have detailed flow while runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mcvduHJNC_FjxSuu-CBDoQ.png" /><figcaption>simple data pipeline. incoming request invokes new job and it’s result submits somewhere</figcaption></figure><p>Our system is unstable due to unstable third parties, also, most of them have rate limiters and so on.</p><p>So, logs coverage would be good enough to track lifecycle.</p><p>Let’s implement some service that takes requests from incoming users to fetch some data from requested source.</p><p>Remember, we want to send out logs to console and database to querying the data.</p><p>First of all, let’s implement simple business flow and having complete app we will update it with Logstage.</p><p>For not wasting your time, I have already implemented basic application with domain logic.</p><blockquote>Please, <a href="https://github.com/7mind/logstage-sample/tree/stage1/domain">checkout on this branch</a>, take a look at the code and will continue our journey.</blockquote><h3><strong>Logstage init</strong></h3><p>Please, open a <em>build.sbt</em> file and add appropriate library dependencies for logstage.</p><pre><em>libraryDependencies </em>++= <em>Seq</em>(<br>  R.<em>fundamentals_bio</em>,<br>  IzumiDeps.R.<em>zio_core</em>,<br>  IzumiDeps.R.<em>zio_interop</em>,<br>  <br>  //logstage<br>  R.<em>logstage_rendering_circe</em>,<br>  R.<em>logstage_core<br></em>)</pre><p>Let’s create simple <em>trait WithLogger, </em>which will contain <strong>logger </strong>and it’s configuration.</p><pre>trait WithLogger {<br>   val <em>logger</em>: IzLogger = ???<br>}</pre><p>We need to implement our console sink with rendering policies.</p><p>We want to have colored messages and with plain text. After bringing all things together, we have a logger and we can use it.</p><pre>trait WithLogger {<br>  val <em>textSink </em>= ConsoleSink.<em>text</em>(colored = true)<br><br>  val <em>sinks </em>= <em>List</em>(<em>textSink</em>)<br><br>  val <em>logger</em>: IzLogger = IzLogger.<em>apply</em>(Trace, <em>sinks</em>)<br>}</pre><p>Let’s replace after <strong>printlns</strong> inside <strong>AdReportService </strong>to be sure that logger is configured properly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16081f30b0496ed3fa7d37737075f482/href">https://medium.com/media/16081f30b0496ed3fa7d37737075f482/href</a></iframe><p>Let’s put into info something and will execute our program.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a68e4921d63b608100418d0fe1592615/href">https://medium.com/media/a68e4921d63b608100418d0fe1592615/href</a></iframe><p>Yeah! We managed to set up the logger within less than 2 minutes. Quite simple, doesn’t it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LZpzDwGVPLQ5z8VCHvJGBQ.png" /></figure><p>Now you can see that our pipeline notify us about each step.</p><p>Let’s update functions of our service with logs to have more details about process. To simulate pulling and pushing let’s add some logged logic.</p><p>Here are updated methods for pulling and pushing data with logger:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbc43e32c9edd0b7b002ddafb7afe631/href">https://medium.com/media/dbc43e32c9edd0b7b002ddafb7afe631/href</a></iframe><p>Please note, that each pulling job requests user id and source id. So, we could filter our queries per each user, per each platform or event per user and platform together!</p><p>Let’s create custom logger inside this function and add key value pairs. Thanks for <strong>method granularity </strong>feature, we can do it very easy.</p><p>Method granularity helps us to define <strong>context </strong>of current flow and track the state for requested user or ad platform</p><blockquote>Also, we can wrap into effect our logger to have better syntax and use the same effect as inside your app at all.</blockquote><pre>val granularLog = <br>  LogBIO.fromLogger[IO](logger(<br>    &quot;userId&quot; -&gt; userId.value, <br>    &quot;ad&quot; -&gt; adPlatform.toString)    <br>  )</pre><p>And let’s put some messages over logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95215e63f6b4e4a4eaa1ecfd9c99764f/href">https://medium.com/media/95215e63f6b4e4a4eaa1ecfd9c99764f/href</a></iframe><p>As you can see, now we have more detailed info for each context and even opening full text search during debugging could navigate you in your output without any complexity!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VuhmTrCgl3nc0xDjf0mv4Q.png" /></figure><blockquote>If you missed out something please <a href="https://github.com/7mind/logstage-sample/tree/stage2/logstage-init">checkout into this branch</a>. It contains all code above. Next step is to write custom sink to flush entries.</blockquote><h3>DB logger sink</h3><p>Let’s write our mocked sink that will simulate writing to db and rendering policy will be json.</p><p>Add doobie dependencies to your <em>build.sbt</em>:</p><pre>//postgres<br>&quot;org.tpolecat&quot; %% &quot;doobie-core&quot;      % &quot;0.6.0&quot;<br>, &quot;org.tpolecat&quot; %% &quot;doobie-hikari&quot;    % &quot;0.6.0&quot;<br>, &quot;org.tpolecat&quot; %% &quot;doobie-postgres&quot;  % &quot;0.6.0&quot;</pre><p>Run your db and execute this statement to create table for logs:</p><pre>CREATE TABLE logstage_out (<br>  at timestamp without time zone NOT NULL DEFAULT now(),  <br>  payload jsonb NOT NULL<br>);</pre><pre>CREATE INDEX logstage_out_payload_json ON logstage_out USING GIN (payload jsonb_ops);</pre><p>And create insert statement for our logs. Having Postgres, we can even put GIN index for our jsonb logs to optimize a little bit</p><p>Here is a code of your sink:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e34921419ad487c65d40ebd9201ac04/href">https://medium.com/media/2e34921419ad487c65d40ebd9201ac04/href</a></iframe><p>Also, don’t forget to add this sink for our logger:</p><pre>val <em>textSink </em>= ConsoleSink.<em>text</em>(colored = true)<br><br>val <em>dbSink</em>: DBJsonSink = new DBJsonSink(<em>blockingIO</em>)<br><br>val <em>sinks </em>= <em>List</em>(<em>textSink</em>, <em>dbSink</em>)</pre><p>Also, I have implemented batching to prevent frequent writing to DB.</p><p>Let’s run our app and let’s query rows from our table. Nice! All our messages are flushed inside the table.</p><p>Here is a content of destination table. You can find there jsonified logs and timestamps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JzkVvzXhzjR4F1PtFsEWMg.png" /></figure><blockquote>You can <a href="https://github.com/7mind/logstage-sample/tree/stage3/db-sink">checkout this branch</a> to have the latest changes and don’t forget to refresh your sbt after multiple updates of build.sbt file</blockquote><h3>Querying</h3><p>As we managed to fill the table with real data, let’s query some result from it.</p><p><strong>Use case 1: Query all actions for specific user and platform</strong></p><pre>select <br>   jsonb(payload-&gt;&gt;&#39;text&#39;)-&gt;&gt;&#39;message&#39; <br>from <br>   logstage_out<br>where <br>   jsonb(payload-&gt;&gt;&#39;context&#39;)-&gt;&gt;&#39;ad&#39; = &#39;GoodFellows&#39;and <br>and  <br>   jsonb(payload-&gt;&gt;&#39;context&#39;)-&gt;&gt;&#39;userId&#39; = &#39;2&#39;</pre><p>And the output is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*dbObzzxGbLYNnjAzBXJ6oA.png" /><figcaption>my output is absolutely simple, but I hope you will have more details there :)</figcaption></figure><p><strong>Use case 2: Select user ids who had rate limiter exception</strong></p><pre>select <br>  DISTINCT jsonb(payload-&gt;&gt;&#39;context&#39;)-&gt;&#39;userId&#39;::text <br>from <br>  logstage_out <br>where <br>  (jsonb(payload-&gt;&gt;&#39;text&#39;)-&gt;&gt;&#39;message&#39;)::text <br>  ilike &#39;%rate limit%&#39;</pre><p>And you have immediate output of unique items:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*lZ8Il91dzlEF6q0zoAA62A.png" /></figure><p>All what you need to track can be fetched easily from your logs.</p><p>Thank you for patience and I hope that you enjoyed with logstage and would be happy to use it. Stay tuned with our new tools and please welcome to contribute or request for issues and enhancements.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ef27e67e7ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LogStage: zero-cost structured logging in Scala. Part 1: Overview]]></title>
            <link>https://medium.com/@rtwnk/logstage-zero-cost-structured-logging-in-scala-part-1-overview-d39e56d4acf2?source=rss-f8ffed390905------2</link>
            <guid isPermaLink="false">https://medium.com/p/d39e56d4acf2</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Maksym Ratoshniuk]]></dc:creator>
            <pubDate>Fri, 19 Jul 2019 13:11:10 GMT</pubDate>
            <atom:updated>2019-07-19T13:20:44.651Z</atom:updated>
            <content:encoded><![CDATA[<p>7Mind glad to present new tool for the best logging ever. In this article, I will tell about core features of structured logging, why do we need it and how it could help us. And also, how Logstage can help you to speed up productivity and increase transparency of your applications’ lifecycle. Here is <a href="https://izumi.7mind.io/latest/release/doc/">an official page of Logstage</a>, where you can find up-to-date information</p><h3>Yet another logger? 🧐</h3><p>Yes! There is a plenty of logging solutions (Airframe, Scribe, slf4j, etc). But nobody can output logs in a structured way.</p><h3>So what is structured logging?</h3><p>Logging should be well-readable by humans and machines. But it’s too hard satisfy both sides. For machines logs can be in XML, JSON formats, for example. People prefers plain text which is absolutely hard for querying by searching engines. The goal of structured logging is to solve these sorts of problems and allow additional analytics.</p><p>Common use cases: <br>● tracing;<br>● the liveness of your system; <br>● metrics:<br> ○ DB calls; <br> ○ HTTP flow; <br> ○ payment processing; <br> ○ scheduling (CRON jobs, spark, etc); <br>● analytics:<br> ○ user behavior; <br> ○ A/B testing;</p><p>We always write code…</p><pre>val user = &quot;JohnDoe&quot;<br>logger.debug(s&quot;Received a message from $user&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*407Q5R-PFziRdYWa2Pwu_A.png" /><figcaption>…that is always structured</figcaption></figure><blockquote><strong>“Logger should be easy to read and easier to deal with”</strong></blockquote><p>Here is a basic example of it’s usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/118955efc2c2204348b0820259c41fdf/href">https://medium.com/media/118955efc2c2204348b0820259c41fdf/href</a></iframe><p>And the console colored output is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vAkUV_dK0V2zE5WTcmgHAA.png" /></figure><p>Or JSON for your search engines, respectively:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*61CANDkiJ8BKBn5tPHR-Ag.png" /></figure><h3>Welcome LogStage!</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FzBhZiVNNQjfTG%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FzBhZiVNNQjfTG%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FzBhZiVNNQjfTG%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/24a4822554e6bb64b076f097c6606ef3/href">https://medium.com/media/24a4822554e6bb64b076f097c6606ef3/href</a></iframe><p><a href="https://github.com/7mind">7mind</a> glad to present first-class logging framework for Scala.</p><p>Let’s define main features and compare with other frameworks.</p><h3><strong>Features</strong></h3><p><strong>1. Macro-based structuring and context extraction</strong></p><p>You can build log messages with string interpolation where logger will output:</p><ul><li>argument names, types (ordered);</li><li>static information (file, line, class, function);</li><li>static part of your log message (<strong>message template</strong> or <strong>interpolation context*</strong>);</li></ul><p><em>Interpolation context can help you query messages with the same structure but different parameter values to fetch information for specific API, for example.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwyqByu5Ara42jIsWxKJnA.png" /></figure><p><strong>2. Aliasing for references</strong></p><p>You can replace variables and structures names by yours:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5YG0KNgBu1Z16G_VLiEO8A.png" /><figcaption>Much better, eh?</figcaption></figure><p><strong>3. Dynamic context &amp; method granularity</strong></p><p>For experienced developers, definition of MDC is known. This means, that you can fill your logger with key-value context.</p><p>Logstage allows you to do it per each class, per each function. Maximum granularity and it’s completely up to you!</p><p>Here is an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aecc34c68e0b17517f6d20b3750d68d3/href">https://medium.com/media/aecc34c68e0b17517f6d20b3750d68d3/href</a></iframe><p>As you can see, you modify logger by custom data (requested user id and it’s company and method name just if you need, for instance). And output is follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QccrVlHx0HYUDxJ2FZoAOA.png" /></figure><p>And later, if you will have some issues with specific user during revoking token, just query by it’s id and api name and you will find it.</p><p><strong>4. SL4J backend</strong></p><p>A drop-in replacement for LogBack, route your legacy routes also!</p><p><strong>5. Production readiness out-of-box</strong></p><p>Izumi Logstage gives you list of needed configurations to start using logger immediately:</p><ul><li>console sink;</li><li>plain text or JSON rendering for messages;</li><li>asynchronous wrappers for sinks (single worker thread at the moment);</li><li>ZIO, Cats and Monix Support<br>(<strong>Fiber id tracking</strong> also supported)</li></ul><p><strong>6. Automatic structure identifiers</strong></p><p>As we wrote in first feature, interpolation context could be very helpful. Here is an example of output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*14TSUcY4RKXV91QLOoqGZw.png" /></figure><p>The last key `<strong>@template`</strong> in JSON above refers exactly to interpolation context. So, to dump list of specific log message, just filter by this template and vulnerable part of your program <strong>will be found as fast as possible</strong>!</p><p><strong>7. Modular</strong></p><p>You can fully customize your logger by your rendering policies (with colors or not, rendering layout, plain text or json)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae8c0d89c5855ea3f08aea31f86d10d3/href">https://medium.com/media/ae8c0d89c5855ea3f08aea31f86d10d3/href</a></iframe><p><strong>8. DI — friendly</strong></p><p>You can use your lovely DI framework if you prefer to configure your logger with different configurations.<br>For example, you can minimize your router for local usage purpose and have powerful tool while running on live:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6b78af53262c5c3538c8969cc330e22/href">https://medium.com/media/a6b78af53262c5c3538c8969cc330e22/href</a></iframe><p><strong>9. Almost no dependencies</strong><br>No more Jar hells while using log libs!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xkr3-NO-RiYgs52xPKGlHQ.png" /><figcaption>Please, don’t tell me that you have never been in the same situation</figcaption></figure><p>In next article we will dive into coding. I will show you real usage case of logger. We will define custom Database Sink to sending logs there and how we could extract structured log data and work with it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d39e56d4acf2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>