uber-go / zap

Blazing fast, structured, leveled logging in Go.

Home Page:https://pkg.go.dev/go.uber.org/zap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ZapProduction losing log entries

matsuev opened this issue · comments

Description

The first 99 entries in log are in order.
After that, skipping of records starts.
Next entries follow in increments of 100.

To Reproduce

package main

import "go.uber.org/zap"

func main() {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	for i := 0; i < 1000; i++ {
		logger.Info("message", zap.Int("id", i))
	}
}

Output is

...
{"level":"info","ts":1674208148.3000069,"caller":"sandbox/main.go:10","msg":"message","id":94}
{"level":"info","ts":1674208148.3000133,"caller":"sandbox/main.go:10","msg":"message","id":95}
{"level":"info","ts":1674208148.3000247,"caller":"sandbox/main.go:10","msg":"message","id":96}
{"level":"info","ts":1674208148.3000295,"caller":"sandbox/main.go:10","msg":"message","id":97}
{"level":"info","ts":1674208148.3000345,"caller":"sandbox/main.go:10","msg":"message","id":98}
{"level":"info","ts":1674208148.3000383,"caller":"sandbox/main.go:10","msg":"message","id":99}
{"level":"info","ts":1674208148.3001235,"caller":"sandbox/main.go:10","msg":"message","id":199}
{"level":"info","ts":1674208148.3001587,"caller":"sandbox/main.go:10","msg":"message","id":299}
{"level":"info","ts":1674208148.3001828,"caller":"sandbox/main.go:10","msg":"message","id":399}
{"level":"info","ts":1674208148.3002057,"caller":"sandbox/main.go:10","msg":"message","id":499}
{"level":"info","ts":1674208148.3002257,"caller":"sandbox/main.go:10","msg":"message","id":599}
{"level":"info","ts":1674208148.3002477,"caller":"sandbox/main.go:10","msg":"message","id":699}
{"level":"info","ts":1674208148.3002734,"caller":"sandbox/main.go:10","msg":"message","id":799}
{"level":"info","ts":1674208148.3003035,"caller":"sandbox/main.go:10","msg":"message","id":899}
{"level":"info","ts":1674208148.3003457,"caller":"sandbox/main.go:10","msg":"message","id":999}

My system
Linux code 4.19.0-23-amd64 # 1 SMP Debian 4.19.269-1 (2022-12-20) x86_64 GNU/Linux
go version go1.19.5 linux/amd64

This is expected behavior. The production logger has sampling enabled by default, but you can customize it:

cfg := zap.NewProductionConfig() // use default config
cfg.Sampling = nil // disable sampling
cfg.Sampling = &zap.SamplingConfig{
  Initial:    1000, // allow the first 1000 logs
  Thereafter: 10,   // take every 10th log afterwards
}

etc. Hope that helps!