Go: How Zap Package is Optimized

Vincent
A Journey With Go
Published in
4 min readAug 15, 2019

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

Go ecosystem has many popular loggers and choosing one that you can use across all your projects is essential in order to keep a minimum of consistency. Ease of use and performance are usually the two metrics we consider in a logger. I will review them in Zap, the logger created by Uber.

Core ideas

Zap is based on three concepts that optimizes the performances, the first being:

  • Avoid interface{} in favor of strongly typed design.

This point leads to two other ones:

  • Reflection free. Reflection comes with a cost and could be avoided since the package is aware of the used types.
  • Free of allocation in the JSON encoding. If the standard library is well optimized, allocations here could easily be avoided, as the package holds all types of the parameters sent.

These points come with a small cost for the developer that forces them to declare each type when recording a message:

--

--