Unlock the Power of Structured Logging in Go
When it comes to logging, many developers think it’s a simple task that involves writing a message to the console or a file. However, as we dive deeper, we realize that logging is a complex task that requires careful consideration of log levels, structuring logs, logging to different locations, and adding the right amount of context to logs.
The Benefits of Structured Logging
Structured logging allows your log entries to have a consistent format, making it easy to process and analyze them. With structured logging, you can filter log entries in various ways, such as searching for logs that contain a specific user ID or error message, or filtering out entries that pertain to a specific service. Additionally, structured logs make it easy to derive relevant metrics from them, like billing information.
Why You Need Structured Logging Packages
If you’re using the inbuilt logging library in Go, you may find it difficult to track down logs, as they are unstructured. Structured logging packages, on the other hand, allow you to add more fields to logs, making it easier to query them and debug issues. They also save time when troubleshooting, as structured logs are formatted in JSON, making them easier to read, query, and store.
Comparing Structured Logging Packages in Go
In this article, we’ll examine five popular structured logging packages in Go: Zap, Zerolog, Logrus, apex/log, and Log15. Each package has its strengths and weaknesses, and we’ll explore their features, performance, and use cases.
1. Zap
Zap is a popular structured logging library for Go, developed by Uber. It provides two separate loggers: Logger for high-performance situations and SugaredLogger for ergonomics and flexibility. Zap allows you to customize the exact fields that you want to appear in the logger, and you can add additional structured context to your logs using any SugaredLogger method that ends with w
.
2. Zerolog
Zerolog is a dedicated library for structured JSON logging. It prioritizes performance using a simpler API and provides a global logger that you can use for simple logging. Zerolog supports seven log levels, ranging from trace to panic, and allows for contextual logging through methods on the zerolog.Event type.
3. Logrus
Logrus provides structured logging for Go applications through an API that is compatible with the standard library logger. It’s easy to switch to Logrus if you’re already using the stdlib log package, but you need to structure your logs to scale your logging process. Logrus doesn’t output JSON by default, but you can easily change this through the SetFormatter() method.
4. apex/log
apex/log is a structured logging package for Go applications that is inspired by Logrus. It simplifies the Logrus API and provides more handlers for common use cases. apex/log uses the WithFields() method to add context to log entries, and you can set up a custom logger through the Logger type, allowing you to configure the handler and log level.
5. Log15
Log15 aims to produce logs that are easily readable by both humans and machines. It uses a simplified API that forces you to log only key/value pairs, and defaults its output formatting to logfmt, but this can easily be changed to JSON. Log15 provides a range of handlers, including the MultiHandler() method, which allows you to dispatch each log entry to all registered handlers.
Performance Comparison
Using the benchmarking suite in the Zap repository, we observed that Zerolog and Zap are the two most performant solutions at the time of writing. However, it’s essential to run the benchmarking suite on your machine with the latest versions of each library to verify these results.
Conclusion
In this article, we’ve explored five libraries for implementing a structured logging approach in Go applications. Each library provides features like JSON logging, log levels, and the ability to log to several locations, making them suitable logging solutions for any project. When choosing a library, consider factors like performance, API, and use cases to select the best fit for your project. Happy coding!