A Comprehensive Guide to Structured Logging in Golang

Structured logging involves producing log records in a well-defined format (usually JSON), which adds a level of organization and consistency to application logs, making them easier to process. Such log records are composed of key-value pairs that capture relevant contextual information about the event being logged, such as the severity level, timestamp, source code location, user ID, or any other relevant metadata.

This article will delve deep into the world of structured logging in Go, with a specific focus on a recently accepted slog proposal which aims to bring high-performance structured logging with levels to the standard library.

We will begin by examining the existing log package in Go and its limitations, then do a deep dive on slog by covering all its most important concepts. We will also briefly discuss some of the Go ecosystem’s most widely-used structured logging libraries.

The standard library log package

Before we discuss the new structured logging proposal, let’s briefly examine the standard library log which provides a simple way to write log messages to the console, a file or any type that implements the io.Writer interface. Here’s the most basic way to write log messages in Go:

package main

import "log"

func main() {
    log.Println("Hello from Go application!")


2023/03/08 11:43:09 Hello from Go application!

The output contains the log message and a timestamp in the local time zone indicating the entry’s generated. The Println() method is one of methods accessible on the preconfigured global Logger, and it prints to the standard error. The following other methods are available: