Best Logging Library for Golang
Tags: go, logging
Published: 2023-02-12 11:24:47

## Introduction

I recently conducted a poll in the Go community regarding which logging library they use in their personal or professional project. The results were interesting. In this post I have jotted down the most favorite logging libraries and here is my opinion about them.

## Poll Results

Here are the poll results which I conducted on [r/golang](https://www.reddit.com/r/golang/), [The Go Programming Language](https://twitter.com/i/communities/1493637136502960134) community on Twitter and [my LinkedIn following](https://www.linkedin.com/in/sntshk/).

<img src="https://d2kbulfv42d43j.cloudfront.net/2022/Q4/reddit-poll-results.png" caption="Poll results on r/golang" alt="Poll results on r/golang" position="center" />

<img src="https://d2kbulfv42d43j.cloudfront.net/2022/Q4/twitter-poll-results.png" caption="Poll results on Twitter" alt="Poll results on Twitter" position="center" />

<img src="https://d2kbulfv42d43j.cloudfront.net/2022/Q4/linkedin-poll-results.png" caption="Poll results on LinkedIn" alt="Poll results on LinkedIn" position="center" />

## Analysis of Results

### Selection of the candidates

For choosing the candidates for the poll, I didn't do any thorough research. I was looking for a library to use in my [project at work](https://gitlab.com/nunet/device-management-service), and I ended up at [sirupsen/logrus](https://github.com/sirupsen/logrus) which was already being used by one of the dependencies in that project. 

After reading the README of the logrus, I noted it was in maintenance mode and no new feature was actively being developed. I still decided to use this library because logrus is a mature project and it has all the features which a logging library should have.

logrus README recommended using other libraries such as [Zerolog][zerolog], [Zap][zap], and [Apex][apex].

[zerolog]: https://github.com/rs/zerolog
[zap]: https://github.com/uber-go/zap
[apex]: https://github.com/apex/log

I started a poll on r/golang with these four candidates, but also came to know about [glog](https://github.com/golang/glog) which was a go port of a C++ project by Google. I used that option in the poll conducted on LinkedIn. 

I haven't personally used any one of them yet.

### logrus

Let's talk about logrus first, it was the first library I landed upon when looking for one. The reason I looked at it first is that logrus was already one of the indirect dependencies of one of the dependencies in my project.

logrus stands 2nd in the place. zerolog is faster than logrus but there's a reason it stands seconds among 4. It has been there for a while.

### zerolog

The main reason you'd want to use zerolog if you are switching from logrus is the performance. [Go](/tags/golang/) is blazingly fast; but if your logger is making it slow, your application gets no benefit from being implemented in Go. 

This is what zerolog's README says:

> The zerolog package provides a fast and simple logger dedicated to JSON output.

> Zerolog's API is designed to provide both a great developer experience and stunning performance. Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection.

### zap

**zap** is probably the most loved logging library for Go. Among **817** people who have voted on r/golang, **315** voted for zap.

According to the stats on their GitHub, zap is the fastest logger available there. 

Also, zap only supports the two most recent minor versions of Go. So you might be locked with an older version of zap if your project for some reason is not able to update to the latest version of Go.


## Conclusion

To have an opinion, I feel that I use them all before coming to a conclusion. But after this poll, I think I'm going to start using zap right away. What is your opinion? Why factor was responsible when you were choosing your logging library.