Home

Awesome

Benchmarking logging libraries for Go

I compared a varity of logging libraries for Go, and performed 2 types of tests for 2 logging formats.

Types of test:

Formats:

Running the tests

On a terminal, just execute:

make

Results

I ran these tests on Mac OSX 10.11.4 using 1.6 GHz Intel Core i5 Macbook Air with 8 GB 1600 MHz DDR3 memory.

Overall, Go kit performed the best, though it has a quirky way of specifying levels.

I was surprised with log15 because it seemed not to take advantage of the 2 or 4 goroutines. I believe this is because it probably performs most of its operations inside some mutex-protected execution path right before flushing the log to the output stream.

When it comes to negative tests for JSON, all loggers made too many allocations for my taste. This is especially bad if you carpet bomb your program with debug-level log lines but want to disable it in production: it puts too much unecessary pressure on GC, leads to all sorts of problems including memory fragmentation. This is something I would like to see improved in future versions!

Take 2 (September 2017)

Two years later, same test ran with Go 1.9 on Mac OSX 10.12.6 using 2.7GHz Intel Core i7 Macbook Pro with 16 GB 2133 MHz LPDDR3 memory.

Overall, zerolog performed the best by a substantial margin with a constant 0 allocations for both text and JSON (output is always JSON) with positive and negative tests.

benchstat

TextPositive

testop timeop alloc szop alloc count
GokitTextPositive-4442ns ± 4%256B ± 0%4.00 ± 0%
GologgingTextPositive-4628ns ± 1%920B ± 0%17.0 ± 0%
Log15TextPositive-43.60µs ± 3%1.12kB ± 0%24.0 ± 0%
LogrusTextPositive-4665ns ± 2%320B ± 0%15.0 ± 0%
SeelogTextPositive-42.18µs ± 1%440B ± 0%11.0 ± 0%
ZerologTextPositive-4130ns ± 4%0.00B0.00

TextNegative

testop timeop alloc szop alloc count
GokitTextNegative-416.7ns ± 1%32.0B ± 0%1.00 ± 0%
GologgingTextNegative-461.4ns ± 1%144B ± 0%3.00 ± 0%
Log15TextNegative-4142ns ± 3%128B ± 0%2.00 ± 0%
LogrusTextNegative-40.98ns ± 4%0.00B0.00
SeelogTextNegative-422.5ns ± 2%48.0B ± 0%2.00 ± 0%
ZerologTextNegative-44.34ns ± 0%0.00B0.00

JSONPositive

testop timeop alloc szop alloc count
GokitJSONPositive-41.42µs ± 4%1.55kB ± 0%24.0 ± 0%
Log15JSONPositive-46.56µs ± 1%2.01kB ± 0%30.0 ± 0%
LogrusJSONPositive-41.81µs ± 3%2.45kB ± 0%33.0 ± 0%
ZerologJSONPositive-4195ns ± 3%0.00B0.00

JSONNegative

testop timeop alloc szop alloc count
GokitJSONNegative-427.3ns ± 2%128B ± 0%1.00 ± 0%
Log15JSONNegative-4189ns ± 2%320B ± 0%3.00 ± 0%
LogrusJSONNegative-4257ns ± 2%752B ± 0%5.00 ± 0%
ZerologJSONNegative-46.39ns ± 2%0.00B0.00

Raw data

TextPositive

testopsns/opbytes/opallocs/op
BenchmarkGokitTextPositive-420000000428 ns/op256 B/op4 allocs/op
BenchmarkGologgingTextPositive-410000000621 ns/op920 B/op15 allocs/op
BenchmarkLog15TextPositive-420000003612 ns/op1120 B/op24 allocs/op
BenchmarkLogrusTextPositive-410000000657 ns/op320 B/op10 allocs/op
BenchmarkSeelogTextPositive-430000002197 ns/op440 B/op11 allocs/op
BenchmarkZerologTextPositive-450000000125 ns/op0 B/op0 allocs/op

TextNegative

testopsns/opbytes/opallocs/op
BenchmarkGokitTextNegative-450000000016.7 ns/op32 B/op1 allocs/op
BenchmarkGologgingTextNegative-410000000060.8 ns/op144 B/op2 allocs/op
BenchmarkLog15TextNegative-450000000146 ns/op128 B/op1 allocs/op
BenchmarkLogrusTextNegative-4100000000001.02 ns/op0 B/op0 allocs/op
BenchmarkSeelogTextNegative-430000000022.1 ns/op48 B/op2 allocs/op
BenchmarkZerologTextNegative-420000000004.34 ns/op0 B/op0 allocs/op

JSONPositive

testopsns/opbytes/opallocs/op
BenchmarkGokitJSONPositive-450000001398 ns/op1552 B/op24 allocs/op
BenchmarkLog15JSONPositive-410000006599 ns/op2008 B/op30 allocs/op
BenchmarkLogrusJSONPositive-450000001761 ns/op2450 B/op33 allocs/op
BenchmarkZerologJSONPositive-430000000195 ns/op0 B/op0 allocs/op

JSONNegative

testopsns/opbytes/opallocs/op
BenchmarkGokitJSONNegative-430000000027.0 ns/op128 B/op1 allocs/op
BenchmarkLog15JSONNegative-430000000188 ns/op320 B/op3 allocs/op
BenchmarkLogrusJSONNegative-430000000255 ns/op752 B/op5 allocs/op
BenchmarkZerologJSONNegative-410000000006.26 ns/op0 B/op0 allocs/op