So you've written a logging library?
That does not impress me much.
binlog is a binary logging library for Go. It is likely one of the fastest logging implementations in the Go ecosystem, or close to it.
The original idea is here: ScottMansfield/nanolog#4
binlog is useful when the standard log package is too slow and adds noticeable latency to a service. Binary logs are smaller, and encoding is faster. You only pay for the dynamic data that you send.
Logging a constant string with no variadic arguments, where the logger only writes the hash of the string to the binary stream, takes about 15 ns. Each additional integer argument costs about 10 ns for a constant and about 20 ns for a non constant value.
binlog.Log() is about 3x faster than fmt.Fprintf() for an empty string and about 30x faster for a call with four arguments. A typical call to binlog.Log() is faster than a map[string]string access. binlog is fast enough for a busy HTTP server.
The benchmark results below come from binlog_test.go on an older machine (see docs/BENCH.md for a recent baseline). In practice, binlog is at least 2x faster than other Go logging libraries that I have tested. It is also much faster than popular loggers such as Zap and Logrus. Its API performance is reasonably close to C++ binary loggers such as NanoLog.
$ ./testall.sh
goos: linux
goarch: amd64
pkg: binlog
BenchmarkEmptyString-4 100000000 14.2 ns/op
BenchmarkSingleInt-4 50000000 26.1 ns/op
Benchmark2Ints-4 30000000 37.0 ns/op
Benchmark3Ints-4 30000000 49.4 ns/op
PASS
coverage: 86.2% of statements
ok binlog 5.324s
Warning! This code pushes Go close to its limits. It uses unsafe pointers, AST walking, StringHeader, and several techniques that are explicitly discouraged in normal Go code. If you prefer a more conventional design and are willing to trade performance for cleaner code, see: https://github.com/ScottMansfield/nanolog
package main
import (
"bytes"
"github.com/larytet/binlog"
)
func main() {
var buf bytes.Buffer
base, size := binlog.GetSelfTextAddressSize()
logger := binlog.Init(&buf, &binlog.WriterControlDummy{}, base, size)
logger.WriteHeader() // optional: the stream then describes itself
logger.Log("Hello %d %s %g", 10, "world", 2.5)
}Supported verbs: %d %i %x %c for integers (8 to 64 bits, signed or unsigned, and runes), %f %e %g for
float32 and float64, %s for strings of any length, and %%. Other verbs and verbs that do not fit the
argument type make Log() return an error and write nothing.
binlogdump prints a binary log as text. It needs the executable that wrote the log, and the sources of that
executable on the same machine (the format strings and argument types are read from them with go/packages):
go install github.com/larytet/binlog/cmd/binlogdump@latest
binlogdump ./myservice service.binlog # stream with a header
binlogdump -flags 8 ./myservice service.binlog # stream without a header: 8 = timestamps
Several streams can be given. -sort timestamp or -sort index merges them into one list in that order
(-window N is the number of entries held back, see binlog.Merge), for example the logs of several processes.
-format ndjson prints one JSON object per entry, and -format es-bulk -index NAME adds the Elasticsearch bulk
action line, so that the output can be sent to _bulk as it is.
From Go: ast.GetIndexTable(executable) returns the table for binlog.DecodeNextFlags.
ast.LoadTable also returns the calls that could not be analysed (a format string that is not a constant,
arguments passed as args..., an interface{} argument) and the calls that cannot share a hash (the same
format text with different argument types).
binlog/io has a lock free ring buffer, Fifo, for the thread which writes the log to a file or a socket.
Several producers and one consumer are supported. io.NewFramer(fifo) is both the writer and the
WriterControl of a logger and makes every log entry one block, so that a consumer never sees half an entry.
Several loggers, each with its own Framer, can share one Fifo:
fifo := io.New(1 << 20)
framer := io.NewFramer(fifo)
logger := binlog.Init(framer, framer, base, size)
// consumer goroutine: fifo.Pop(buf), or binlog.DecodeNextFlags(fifo, ...) since Fifo is an io.ReaderIf the ring is full the entry is dropped and counted (Dropped()).
The goroutine which writes the log to its destination can compress it, the logging call never does:
file, _ := os.Create("service.blz")
zw, _ := io.NewCompressedWriter(file, io.Flate, 0) // io.None stores, io.Flate uses DEFLATE
zw.Write(block) // for example the blocks popped from a Fifo
zw.Close()Blocks are independent and carry a CRC, so a damaged block is reported and the rest of the stream is not
misread. A synthetic stream of repeated entries shrank 5.7 times with Flate in the tests (TestCompressRatio); the ratio of a real log depends on its content, and Flate runs at about 30 MB/s per goroutine. binlogdump reads compressed streams
directly, io.NewCompressedReader does it from Go.
When an application calls binlog.Log(), the logger checks a cache using the offset of the format string in the executable as an index. This step is fast, like 2 opcodes fast.
On a cache miss, Log() collects the required metadata and stores the format string in the cache, called the L1 cache.
On a cache hit, Log() writes the hash of the format string and all variadic arguments to the target io.Writer.
If the string does not come from the executable image, for example if it was allocated on the heap, Log() stores it in a map, called the L2 cache.
The L1 and L2 caches store the data required to decode and format the binary stream later. This includes argument sizes, format verbs, number of arguments, the hash of the format string, and the format string itself.
go get github.com/larytet/binlog (Go 1.27 or later). The library has no dependencies outside the standard library; binlogdump uses golang.org/x/tools/go/packages.
go test ./... runs the unit tests and the end to end tests (about a minute: they build programs and load their sources). docs/ has the requirements, the design, the test plan and the benchmark baseline.
Linux and amd64 are tested. The code reads /proc/self/maps, and binlogdump reads ELF executables. Windows and macOS are not supported.
The code relies on these assumptions:
- Go string literals are located in the same ELF segment.
- ELF provides a unique address for each string literal in the source code.
String deduplication may become a real issue in the future. If Go starts deduplicating strings, the cache key will need to be larger than a single string address. That will hurt performance.
If fast logging matters to you, please comment or vote here: golang/go#28864
Maybe one day the standard log package will cache format strings and support binary output as well.
A Binlog instance is not thread safe: use one instance per goroutine. Instances can share one output through a Fifo (see above).
The application is expected to flush output to a file or to stdout from time to time.
An application can share an io.Writer between multiple binary loggers if it implements WriterControl.
You can also add an index or timestamp to all log entries, sort them later, and print them in human readable order. An atomic counter costs about 25 ns per call, since Go sync/atomic is not especially fast.
This logger will not work well for applications that build format strings dynamically, as in the example below. In that case, performance will be closer to Zap and similar loggers.
{
fmtString := fmt.Sprintf("%s %%d", "Hello")
err := binlog.Log(fmtString, value) // relatively slow "L2 cache" is used here
}The following formats are not supported: "%v", "%T", "%p".
Offline decoding needs the executable and its sources, not the sources alone. A stripped executable is fine, a Go executable built with -trimpath is not (the file table has no absolute paths).
Presentation: https://docs.google.com/presentation/d/1WuY5eifDb0XcCtYMhoj6CdqNL2_AVCRPVpbvmZUwtBg
More benchmark for different logging frameworks (Spoiler: doing nothing NOP loggers require 100ns/op)
- https://gist.github.com/Avinash-Bhat/48c4f06b0cc840d9fd6c
- https://stackoverflow.com/questions/10571182/go-disable-a-log-logger
- https://blapid.github.io/cpp/2017/10/31/llcpp-a-quest-for-faster-logging-part-2.html
- gabime/spdlog#942
- https://github.com/zuhd-org/easyloggingpp
- https://godoc.org/github.com/alexcesaro/log/stdlog
- https://github.com/hashicorp/logutils
- https://github.com/romana/rlog
- https://github.com/morganstanley/binlog C++ binlog
Golang related stuff
- golang/go#28864
- https://medium.com/justforfunc/understanding-go-programs-with-go-parser-c4e88a6edb87
- https://stackoverflow.com/questions/50524607/go-lang-func-parameter-type
- https://stackoverflow.com/questions/46115312/use-ast-to-get-all-function-calls-in-a-function
- uber-go/zap#653
- https://groups.google.com/forum/#!topic/golang-nuts/Og8s9Y-Kif4
- https://github.com/elazarl/gosloppy - probably contains more example of Go parsing
Done since the first version: offline decoding, a stream header, floats and runes, signed integers, empty and
long strings, the FIFO (several producers), binlogdump. See docs/REQUIREMENTS.md for the status of every
requirement.
Open:
- Integration with Megalog (its interface is not defined yet).
- The speed on the benchmark machine is 3 to 4 times slower than the numbers at the top (older, faster
hardware).
docs/BENCH.mdhas the current baseline. The FIFO write path is about 40 ns slower than abytes.Bufferwrite. - The runtime logger does not detect two calls with the same format text and different argument types (the offline table reports them as a conflict).