Zap / Logrus for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Logging in Go: Zap and Logrus



Logging is a critically important component of any serious application. It allows you to track the program's state, record errors, analyze performance, and debug code in a production environment. The Go standard library has the log package, but it has limited capabilities: no logging levels (except primitive ones), no structured output, no flexible formats. To solve these problems, two powerful libraries were created: Zap (from Uber) and Logrus (one of the first popular loggers).



In this article, we will thoroughly examine both libraries, their methods, usage examples, and best practices. You will learn how to set up structured logging, how to work with levels, how to add contextual fields, and how to avoid common mistakes.



What is Zap?

Zap is a high-performance structured logging library from Uber. It is focused on speed and minimal memory allocation. Zap supports two modes of operation: SugaredLogger (convenient, but slightly slower) and Logger (maximum performance).



What is Logrus?

Logrus is one of the first and most popular logging libraries in Go. It provides a simple API, support for levels, formatters (JSON, Text), and hooks for integration with external systems (e.g., Sentry, Slack). Logrus is easier to learn but is inferior to Zap in performance.



Installation

Installation of both libraries is done in the standard way via go get:



// Installing Zapgo get -u go.uber.org/zap

// Installing Logrusgo get -u github.com/sirupsen/logrus


After installation, you can import them into your project:



import (    "go.uber.org/zap"    "github.com/sirupsen/logrus")


Quick Start with Code Example



Example with Zap

package main

import ( "go.uber.org/zap")

func main() { // Create a production logger (high performance) logger, _ := zap.NewProduction() defer logger.Sync() // flush buffer before exit

// Log a message with fields logger.Info("Application started", zap.String("version", "1.0.0"), zap.Int("port", 8080), )

// Use SugaredLogger for convenience sugar := logger.Sugar() sugar.Infow("User logged in", "user", "john_doe", "ip", "192.168.1.1", )}


Example with Logrus

package main

import ( "github.com/sirupsen/logrus")

func main() { // Set output format (JSON) logrus.SetFormatter(&logrus.JSONFormatter{})

// Set logging level logrus.SetLevel(logrus.InfoLevel)

// Log a message with fields logrus.WithFields(logrus.Fields{ "version": "1.0.0", "port": 8080, }).Info("Application started")

// Log an error logrus.WithFields(logrus.Fields{ "user": "john_doe", "ip": "192.168.1.1", }).Warn("Login attempt with incorrect password")}


Main Methods and Functions



1. Zap: NewProduction() / NewDevelopment()

What it does: Creates a pre-configured logger for production or development environments. The production logger uses JSON format and is optimized for speed. The development logger uses colored text output and includes stack traces.

Parameters: None (returns *zap.Logger and an error).

Example:

prodLogger, _ := zap.NewProduction()devLogger, _ := zap.NewDevelopment()


2. Zap: Logger.Info(),

Recommendations