4 Efficient Logging Packages for Your Go Apps
Logging is the technique of keeping records for future purposes in the software development cycle. Logging is critical because logs help with debugging, diagnostics, troubleshooting, and project monitoring.
Sign up forfree
Forgot your password?
Create an account
*Required: 8 chars, 1 capital letter, 1 number
By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you’re able to opt-out any time.

Logging in Go
The Go standard librarycontains alogpackage rich in functionality. It handles various logging levels and basic logging-related methods you’ll need for your application. However, thelogpackage might not be the best choice if your app is complex and you want to prioritize productivity.
Thelogpackage doesn’t provide functionality for structured logs. Structured logging packages provide functionality that simplifies and improves the logging process. The Go ecosystem is home to many such packages.

1. Zap by Uber
Zapis a fast, structured, leveled logging package built by Uber’s open-source team for writing logs in Go. Uber built the Zap package to provide more performant logging than other packages in the Go ecosystem, including thelogpackage.
There are two distinct loggers in the Zap package. TheLoggerfunction handles critical performance cases. TheSugaredLoggeroffers more flexibility with its printf-style API, but it comes with a small tradeoff in performance. Even the slower SugaredLogger package is 4-10 times faster than other structured logging packages.

Run the following on a command line to install the Zap package:
You’ll need a recent version of Go to install and use the Zap package’s functionality successfully.

Theloggervariable is an instance of thezaplogger, and theSugarmethod is a sugared logger instance.
TheInfowmethod writes to the output, and theInfofis the formatting version of theInfowmethod.

2. The Logrus Package
Logrusis a structured logging package for Go apps. Logrus is compatible with the standard library logger, with similar functionality. If you have experience using thelogpackage, you’ll find a suite working with Logrus.
Logrus doesn’t support JSON formatting by default. But you can always use a JSON library like the built-injsonpackage with Logrus’SetFormattermethod.
Logrus supports logging at different levels and, although it’s not as performant as most logging packages, it’s feature-rich and safe.
You can use this command to install Logrus in your working directory:
Here’s an example of logging with the Logrus package.
This code imports the Logrus library and creates an alias for it namedlog.In themainfunction, it calls theSetFormattermethod to set a formatter for the logs. You can use theSetOutputmethod to specify where log messages should go; in this case, standard output.
TheSetLevelmethod logs warnings in the specified level or above.
3. ZeroLog Package
ZeroLogis a Zap-inspired, fast, JSON-dedicated library for logging, designed for performance. It uses a unique chaining API that allows Zerolog to write JSON and log events without allocations and reflections.
Zerolog aims to provide an easier-to-use API and higher performance while keeping the code base and API simple. It focuses on structured logging and you can use theConsoleWritermethod for pretty logging on your console.
There’s low allocation, leveled logging, sampling, hooks, contextual fields, and error logging with optional stack trace features on the Zerolog package. You can also integrate Zerolog with thecontextandhttppackages.
Run this command in the terminal of your workspace to install theZerologpackage.
Here’s a simple example of using the Zerolog package for a simple operation.
TheTimeFieldFormatoption is set to the Unix time format, and thePrintcommand writes the text argument to standard output.
4. The Log15 Package
TheLog15package is a simple opinionated toolkit for human and machine-readable logging with best practices in Go. Log15 models theioandhttppackages from the Go standard library as an alternative to the built-inlogpackage.
The features of the Log15 package include:
You can installLog15to your Go packages with this command.
It’s easy to get started with the Log15 package. Here’s an example of instantiating a logger and logging on info and error levels with the package.
TheserverLogvariable is an instance of the Log15 logger; theNewmethod returns a logger with the context arguments you provide.
TheInfomethod returns an info message, and theErrormethod returns an error message.
Write Useful and Understandable Logs
Logging can be as critical as any other part of the development process. It may seem very easy in the initial stages, but sticking to essential practices may complicate the process. To handle every edge-case and aspect of logging, you should use a logging package to make things easier.
Use logging levels, structure, and context to make your logs understandable and fit for their intended purposes.
Find out how to use string formatting verbs and which Go functions support them.
Your phone is a better editor than you give it credit for.
Revolutionize your driving experience with these game-changing CarPlay additions.
You can block out the constant surveillance and restore your privacy with a few quick changes.
Not all true crime is about hacking, slashing, and gore.
Who asked for these upgrades?