
Structured logging includes producing log data in a well-defined format (normally JSON), which provides a stage of group and consistency to utility logs, making them simpler to course of. Such log data are composed of key-value pairs that seize related contextual details about the occasion being logged, such because the severity stage, timestamp, supply code location, consumer ID, or every other related metadata.
This text will delve deep into the world of structured logging in Go, with a selected concentrate on a not too long ago accepted slog proposal which goals to carry high-performance structured logging with ranges to the usual library.
We are going to start by analyzing the prevailing log
bundle in Go and its limitations, then do a deep dive on slog
by overlaying all its most vital ideas. We will even briefly talk about a number of the Go ecosystem’s most widely-used structured logging libraries.
The usual library log bundle
Earlier than we talk about the brand new structured logging proposal, let’s briefly look at the usual library log
which supplies a easy option to write log messages to the console, a file or any sort that implements the io.Author
interface. Right here’s essentially the most fundamental option to write log messages in Go:
bundle major
import "log"
func major() {
log.Println("Hiya from Go utility!")
}
Output
2023/03/08 11:43:09 Hiya from Go utility!
The output incorporates the log message and a timestamp within the native time zone indicating the entry’s generated. The Println()
methodology is one among strategies accessible on the preconfigured world Logger
, and it prints to the usual error. The next different strategies can be found: