redirecting log output into testing.T.Log

66 views
Skip to first unread message

Patrick Ohly

unread,
Feb 2, 2022, 5:55:39 PM2/2/22
to golang-nuts

Hello!

I'm working on logging in Kubernetes. One of the problems there is that all log output goes through a global logger in the k8s.io/klog package, which writes to stderr. When such code runs in a unit test, that additional output is not associated with the currently running unit test, so the output of `go test` for a failed test case is usually not helpful. It gets even worse when running tests in parallel.

I have a proposal pending for Kubernetes for doing logging via a go-logr/logr.Logger and how to pass that logger instance around through the code base. During tests, each test function then can create such a Logger that writes via t.Log and `go test` output becomes more useful. The actual code that goes into binaries is the same, it just uses different Logger implementations. The downside is that tests have to be modified to set up such per-test log output.

I don't see any other way of doing that, though, because the t instance of a test is by definition specific to each test function and therefore only that function can create the logger and then pass it on. If it was done by some kind of test driver, then there would be no way to pass the right logger into a `func TestFoo(t *testing.T)`.

How are other projects dealing with this? Is "normal" log output ignored and only log output from test code that gets the t parameter is used?

Thanks, Patrick


Reply all
Reply to author
Forward
0 new messages