Skip to content

Conversation

@chaewonkong
Copy link
Contributor

Description

  • Fix LogWithZax function in the benchmark test to log added fields correctly.

Benchmark test function BenchmarkLoggingWithZax in zax_benchmark_test.go will not print added fields to context, unless context is properly provided.

in order to provide fields attached to ctx, the outcome of Set function should be reassigned to ctx.

ctx = Set(ctx, someFields) // assign result of Set to ctx.
logger.With(Get(ctx)...).Info("logging something")

How to test

Just make the benchmark function to print logs:

func BenchmarkLoggingWithZax(b *testing.B) {
	// Create a no-op logger that discards log output
	logger := zap.NewExample()
	logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		enc := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
		return zapcore.NewCore(enc, zapcore.AddSync(os.Stderr), zapcore.InfoLevel)
	}))

	for i := 1; i <= b.N; i++ {
		LogWithZax(logger)
	}
}

Result:

...
{"level":"info","ts":1742721091.128562,"msg":"logging something","field1":"value1","field2":"value2","field3":2}
{"level":"info","ts":1742721091.128564,"msg":"logging something","field1":"value1","field2":"value2","field3":2}
{"level":"info","ts":1742721091.128565,"msg":"logging something","field1":"value1","field2":"value2","field3":2}
{"level":"info","ts":1742721091.128567,"msg":"logging something","field1":"value1","field2":"value2","field3":2}
BenchmarkLoggingWithZax-8          24927             48147 ns/op            1369 B/op          7 allocs/op
PASS
ok      github.com/yuseferi/zax/v2      2.143s

@chaewonkong chaewonkong requested a review from yuseferi as a code owner March 23, 2025 09:12
@yuseferi
Copy link
Owner

Hey @chaewonkong ,

Thank you very much for your contribution!

@yuseferi yuseferi merged commit 7931e87 into yuseferi:master Mar 23, 2025
3 of 4 checks passed
@chaewonkong chaewonkong deleted the fix-benchmark-test branch March 24, 2025 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix benchmark test to log additional fields

2 participants