Member-only story
How To Use Structured JSON Logging in Golang Applications
Structured logs are important for software debugging. Gladly, it is super easy to implement in Golang
Sick and tired of going through unstructured logs to track down a bug? Ever tried forcing a team of developers to write down their error messages in a structured way? Well, you’ve come to the right place in your journey to stop bad software logs.
What Is Structured Logging?
Logging is a very important part of software. We do need logs to keep track of what’s going on in systems and as they grow large it becomes increasingly important.
There are many great tools out there that help us parse logs. I’m personally a great fan of ElasticSearch for instance. But these tools are most of the time depending on a structured log, not a plain text blob mass that may or may not be written in a certain format.
I remember when being assigned a bug to track down, the system had about 30 CRON jobs running some scripts, and all the scripts wrote their output to a single file which was rotated by date. So there was at least some kind of central logging (not the most recommended one, but hey). There were logs for this ancient system, so I went in, duct tape in hand; I was going to patch this bug.
The first thing I met in the logs was horrible: every script printed simple text messages to the log file with no information at all about what service it was. There was no context, no information to reproduce the bugs, nothing. I stared with big watery eyes at this:
"" not allowed
Logs were filling up fast, and the old log files were massive. Not strange since there were about 50–60 bash scripts which were all pretty big and running frequently. It took me quite a while to actually track down the bug, time that could have easily been saved with structured logs.
Structured logs are logs written in a well-known format to help read / query and store them. One of the most common ways is JSON formatted logs.
In these logs, you can specify a few fields that have to be available that will always help to debug. Let’s imagine that this horror system of…