Getting The Most Out of Your Logs with ELK
Tags: microservices, logging, elk, elasticsearch
Published: 2020-04-27 15:06:01

---
title: Getting The Most Out of Your Logs with ELK
published: true
description: How to use ELK for monitoring your production service
tags: microservices,logging,elk,elasticsearch
series: Integrating your Go service with ELK
cover_image: https://i2.wp.com/pmihaylov.com/wp-content/uploads/2020/04/pasted-image-0.png?w=1352
canonical_url: https://pmihaylov.com/analysing-logs-elk/
---

<!-- wp:paragraph -->
<p>When you start developing your application, you typically instrument it with some logging to be able to debug problems later.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some skip it in the development phase, but once the application hits production, then it is crucial to have some logging. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After all, once users complain that something isn't working, how would you be able to find the root-cause?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And although logging proves to be useful, many companies don't really capitalise on its potential as they're still clinging to the classic way of writing freestyle logs and grep-ing them on their prod machines afterwards.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, there is so much more potential that logging holds for monitoring our production systems. In this article, I will show you how to get the maximum value from your logs using the ELK stack.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>The Classic Way of Logging</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What I refer to as the classic way to log looks like this:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">INFO - 2020-01-08 12:34:03 - Received database error with user 53...</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>The amount of debug details present in the logs varies and depends on what the developer decides to include. Sometimes, the logs might include other metadata such as e.g. user's country, endpoint, http method, stack trace, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This all depends on the developer who prints out the log line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So how do you really debug an issue once it occurs?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It typically goes by you ssh-ing to the production machine and fetching the application's logs locally. And then, using the good-old grep, you try to extract the key log lines you need &amp; start tracing what's happened with the IDE on the side to see where that log occurred.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This approach can get you quite far and works well for small to medium applications. But once your application starts evolving, it starts getting in the way.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The Problems with the Classic Approach</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The more complex your system becomes and the more logs it starts popping out, the harder it becomes to identify the problems within your system.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to simply start looking for the problem, you have to login to your prod machine, download the log, filter out all the lines you don't need via some advanced grep usage, open the IDE, get a sheet of paper to "trace the log history", etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This can work well when you have enough time &amp; patience, but imagine that they wake you up during the night and you have to go through this entire drill to simply start looking for the problem.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This disturbance and overhead will be very hard on developers. This is why,  for these occasions, you should have proper tools to quickly filter out the noise &amp; identify the problem fast.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Especially for larger companies, if their service is down for an hour, that might pose huge financial risk.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This problem gets even more problematic once your application starts to scale &amp; you have <a href="https://pmihaylov.com/shared-components-go-microservices/">a ton of micro services</a> set up. That will bring even more noise to your logs and identifying the problem will be even harder.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, now that your team is starting to experience these pains of growth, what options do you have?</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Introducing Structured Logging</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Structured logging is a simple concept, which gives your logs the ability to be parsed &amp; analysed by a monitoring tool.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To put it simply, a structured log is one which has a schema and that schema is typically represented in JSON format. Here's an example:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">{"level":"info","ts":1587291719.4168398,"caller":"my-application/main.go:76","msg":"Inbound request succeeded","endpoint":"/payments/execute","method":"GET","countryISO2":"BG","userID":"42","paymentMethod":"VISA","userType":"business"}</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>What you see in the example above, this time, is that the log is structured in a JSON format with a set of keys and values. Those key-value pairs hold some kind of metadata information such as a user's country, payment method, user type, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is just an example of course. The keys can be domain-specific, based on your application's business logic.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, if the developers are diligent, they can enrich all those logs with the same amount of metadata with the classical logging approach as well.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But the great benefit of this approach, is that now the logs can be processed by other analytical applications. At the very least, you'll be able to grep your applications more easily as you can e.g. easily filter out the fields you don't need based on your log's schema.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, if you use structured logging just to make grep-ing easier, then that's like <a href="https://www.amazon.com/Animal-Farm-George-Orwell/dp/0451526341">using a windmill for milling corn</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to get the most out of structured logging, you need the right tool for the job.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Analysing your Structured Logs using ELK</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>ELK stands for <strong>Elasticsearch</strong>, <strong>Logstash</strong> &amp; <strong>Kibana</strong>. These are a bundle of free tools, all maintained by the same company - <a href="https://www.elastic.co/about/">Elastic</a>. They are typically used together in order to provide you with a monitoring system based on your logs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Learning the details of how these components work together is not as important as understanding the value they bring. You can study the details of how they work and how to set them up later. We have to get you sold first.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The end result, is a monitoring dashboard which looks like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1088,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://i2.wp.com/pmihaylov.com/wp-content/uploads/2020/04/dashboard-view.png?fit=676%2C420&amp;ssl=1" alt="Entire dashboard view in ELK" class="wp-image-1088"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The dashboard is fully customisable and has support for all kinds of views - graphs, histograms, vertical bars, pie charts, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The example I've provided above is of an application which resembles a payment provider and has a couple of endpoints, some of which have errors in them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And the cool thing is that this dashboard is 100% based on those structured logs I showed you previously. ELK simply parses that input &amp; visualises it in this pleasant way.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Having a dashboard like this set up, with a single gaze you can:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Evaluate the success to error ratio</li><li>Evaluate which endpoints have the most errors</li><li>What kind of errors is your application throwing</li><li>Which input parameters (e.g. country, user type, payment method) cause the highest % of errors</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>But there's more to it than simply an overview of your system's status.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Zooming in on your data</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With a single click you can "zoom in" on any input value and see what the data shows for that input. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, if I want to see the success/error details of the endpoint <strong>/payments/authhold</strong>, all I need to do is filter that value: </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1084,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://pmihaylov.com/wp-content/uploads/2020/04/image-1.png" alt="filter by value example" class="wp-image-1084"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And then, the dashboard will refresh and only show you the details of that chosen endpoint.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1086,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://i0.wp.com/pmihaylov.com/wp-content/uploads/2020/04/image-3.png?fit=676%2C315&amp;ssl=1" alt="zoomed view on specific endpoint" class="wp-image-1086"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>But not only can you set up an aggregated view of your logs. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can easily add a detailed view of all your log lines. They are free for you to inspect the same way you do with plain logs &amp; grep-ing. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But still, a bit more convenient:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1087,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="http://pmihaylov.com/wp-content/uploads/2020/04/detailed-logs-view-1024x454.png" alt="Detailed logs view with ELK" class="wp-image-1087"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, which approach do you prefer for finding the root cause of an outage? Especially when they wake you up in the middle of the night.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you've been writing &amp; grep-ing plaintext logs until now, this article should be a mini-industrial revolution for your root cause analysis workflow. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Integrating ELK in your application can make your life and the lives of your teammates much easier. And also pleasant when it comes to debugging issues in production.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, this article only explains how structured logging and ELK work conceptually. If you're hooked, then you should spend some time afterwards to read more about how ELK works and how to integrate it with your application.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Are you interested in a follow-up article, walking you through this? <br>Then let me know in the comments section below.</p>
<!-- /wp:paragraph -->