<?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 syrinsecurity on Medium]]></title>
        <description><![CDATA[Stories by syrinsecurity on Medium]]></description>
        <link>https://medium.com/@syrinsecurity?source=rss-7471cc2dca65------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8hoj90CM49_sKAoqvUVHSA.png</url>
            <title>Stories by syrinsecurity on Medium</title>
            <link>https://medium.com/@syrinsecurity?source=rss-7471cc2dca65------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:00:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@syrinsecurity/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[Golang logging fast and concurrently.]]></title>
            <link>https://medium.com/syrinsecurity/golang-logging-fast-and-concurrently-3780d98fdad8?source=rss-7471cc2dca65------2</link>
            <guid isPermaLink="false">https://medium.com/p/3780d98fdad8</guid>
            <category><![CDATA[concurrent]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[fast]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[syrinsecurity]]></dc:creator>
            <pubDate>Mon, 31 Aug 2020 15:17:51 GMT</pubDate>
            <atom:updated>2020-08-31T16:03:46.018Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang logging concurrently and super fast.</h3><p>Golang as we all know has great concurrency support. We usually use Go to build systems/servers, of course this requires go’s concurrency system. But if what if we want to write to a file how do we do that without data races?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wcGR390lVbZ3vZJC" /><figcaption>Photo by <a href="https://unsplash.com/@ismailenesayhan?utm_source=medium&amp;utm_medium=referral">İsmail Enes Ayhan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Creating a function to open the file and write to it may be the first thing that comes to you head, It was the first thought we had. But this has some sever problems.</p><ol><li><strong>File descriptors! </strong>The last thing we want to do is open up thousands of file descriptors. On Unix based systems there is actually a <strong>finite limit</strong> (1024)of sockets you can have open. The <a href="https://linuxhint.com/linux_ulimit_command/">ulimit </a>command can be used to modify this limit.</li><li><strong>Non consistent write order.</strong> If you call your log function twice, one after the other, then logs may <strong>not be in the order you were expecting</strong>. Because you have multiple goroutines running and calling this function, Go’s runtime is going to wait, which yes is blocking (like all io), this will result in other goroutines <em>writing when they can </em>regardless of code order.</li><li><strong>Slow! And blocking.</strong> Go’s io is all thread safe, this results in waiting for A. the io’s mutex to become unlocked and then B. wait for the file to be written. In a high performance server scenario this kind of behavior is unacceptable.</li></ol><h3>Solution?</h3><p>Okay so there is a lot of problems with the first method. Using a file descriptor globally any better? While it is “better” it still is not a viable solution. So what are our requirements anyway? We want a logging library which is super fast and concurrent, which does not leak file descriptors nor goroutines.</p><p>If we create a custom logger type and give it one single file descriptor and run a method e.g. logger.Write(“log something”) this will still be blocking. Okay? So we can just put it on a goroutine? Wrong. We would then experience the same problems as the first solution we explored.</p><p><strong>Service workers</strong>, which use one goroutine and waits for a log to be parsed onto its <strong>queue </strong>channel. If we used a buffered channel, which can be declared like: <strong>make(chan []byte, 200)</strong>, we will be able to push to the channel in a non blocking way.</p><p>Create a struct called Logger and give it a method called Write. Write should format your “log” and then push it onto the queue. As we are using a buffered queue pushing to the channel will return instantly instead of waiting for it to be “taken out on the other end”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*dWniog7BuaTtzabvegAy8Q.png" /></figure><h4>Looking for something more advanced and feature rich?</h4><p><a href="https://github.com/syrinsecurity/gologger">Gologger </a>is a logging library developed and used internally by the team at <a href="https://syrinsecurity.net/">Syrinsecurity</a>.</p><p><a href="https://github.com/syrinsecurity/gologger">Gologger</a> uses the same concepts we discussed above but provides a nicer and cleaner API which helps you quickly log any kind of data.</p><p>Creating a new logger is as simple as calling gologger.New(“./path-to-file.log”, 3000). gologger.New() will return a gologger.Logger and a error if it encounters any.</p><p>You can use Gologger just like fmt if you like. logger.Write() accepts a array of interfaces just like fmt does. <em>logger.Write(“string data”, 200, “that was a int”, “we can keep on appending more values to this endlessly”, “it will all be formatted with fmt.”)</em></p><p>Need to log JSON? Just call logger.WriteJSON(object) and Gologger will marshal the object for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2d30935705baf2408c2ba411aa13382/href">https://medium.com/media/c2d30935705baf2408c2ba411aa13382/href</a></iframe><h3>You made it!</h3><p>Thank you for reading to the end. I hope you learnt a thing or two and inspired you to go out and write your own logger or use <a href="https://github.com/syrinsecurity/gologger">Gologger</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3780d98fdad8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/syrinsecurity/golang-logging-fast-and-concurrently-3780d98fdad8">Golang logging fast and concurrently.</a> was originally published in <a href="https://medium.com/syrinsecurity">Syrinsecurity</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>