Logging in Go has come a long way. For years, the community relied on the simple standard logCopy package or turned to powerful third-party libraries like zap and zerolog.With the introduction of log/slog in Go 1.21, the language now has a native, high-performance, structured logging solution designed to be the new standard.slogCopy isn’t just another logger; it’s a new foundation that provides a common API (the frontend) that separates logging logic from the final output, which is controlled by various logging implementations (the backend).This guide will take you through slogCopy from its fundamentals to advanced patterns, showing you how to make logging a useful signal for observing your applications.The log/slogCopy package is built around three core types: the LoggerCopy, the HandlerCopy, and the RecordCopy. The LoggerCopy is the frontend you’ll interact with, the HandlerCopy is the backend that does the actual logging work, and the RecordCopy is the data passed between them.A RecordCopy represents a single log event. It contains all the necessary information about the event including:The time of the event.The severity level (INFOCopy, WARNCopy, etc.).The log message.All structured key-value attributes.Essentially, a RecordCopy is the raw data for each log entry before it’s formatted.A HandlerCopy is an interface that’s responsible for processing Records. It’s the engine that determines how and where logs are written. It’s responsible for:Formatting the RecordCopy into a specific output, like JSON or plain text.Writing the formatted output to a destination like the console or a file.The log/slogCopy package includes built-in concrete TextHandlerCopy and JSONHandlerCopy implementations, but you can create custom handlers to meet any requirement. This interface is what makes slogCopy so flexible.The LoggerCopy is the entry point for creating logs, and it’s what provides the user-facing API with methods like Info()Copy, Debug()Copy, and Error()Copy.When you call o...
First seen: 2025-09-12 09:34
Last seen: 2025-09-12 15:54