Repository navigation
Description
As suggested in #23213 (comment), I'm filing a new issue with a proposal for how to improve t.Log and t.Logf.
What version of Go are you using (go version)?
go version go1.9.2
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
I'm on Darwin, amd64, but this affects all systems/architectures.
Motivation
Consider the following (silly) automated tests:
func TestFoo(t *testing.T) {
t.Parallel()
for i := 0; i < 15; i++ {
t.Logf("%d", i)
time.Sleep(3 * time.Second)
}
}
func TestBar(t *testing.T) {
t.Parallel()
for i := 0; i < 15; i++ {
t.Logf("%d", i)
time.Sleep(2 * time.Second)
}
}
func TestBaz(t *testing.T) {
t.Parallel()
for i := 0; i < 15; i++ {
t.Logf("%d", i)
time.Sleep(1 * time.Second)
}
}If I run go test -v, I get no log output until all of TestFoo is done, then no output until all of TestBar is done, and again no more output until all of TestBaz is done. This is fine if the tests are working, but if there is some sort of bug, there are a few cases where buffering log output is problematic:
- When iterating locally, I want to be able to make a change, run my tests, see what's happening in the logs immediately to understand what's going on, hit CTRL+C to shut the test down early if necessary, make another change, re-run the tests, and so on. If
TestFoois slow (e.g., it's an integration test), I get no log output until the very end of the test. This significantly slows down iteration. - If
TestFoohas a bug that causes it to hang and never complete, I'd get no log output whatsoever. In these cases,t.Logandt.Logfare of no use at all. This makes debugging very difficult. - Moreover, not only do I get no log output, but if the test hangs too long, either the Go test timeout kills the test after 10 minutes, or if I increase that timeout, many CI servers will also kill off tests if there is no log output after a certain amount of time (e.g., 10 minutes in CircleCI). So now my tests are killed and I have nothing in the logs to tell me what happened.
Current workarounds
The main workaround is to replace all t.Logf calls with either fmt.Printf or a custom logger. Since we often want to iterate quickly on tests, and since debugging is such an important use case, in practice, that means we never use t.Logf at all.
This seems like a missed opportunity. Go has a lot of powerful testing tools built in, and we usually pass the t variable around to many test methods so they can call t.Fatal(), t.Name(), etc. It would be great if t had a logger built in that we could actually use. Honestly, the only thing it's really missing is some way to configure the logger to write to stdout immediately, rather than buffering.
Possible solutions
Probably the easiest way to improve t.Log and t.Logf is to add a -test.streamlogs option to the go test command. When this option is set, t.Log and t.Logf write to stdout immediately, instead of buffering.
This means the output format with this option set wouldn't be backwards compatible, but the expectation is that this option would only be used for local debugging, and not something used in your CI system.
Activity
randall77 commentedon Apr 21, 2018
I think we can modify
-vto get what you want. For instance:This will print output immediately, and you get something like this:
It's pretty jumbled, but gets you what you want.
I don't think there's any issue with changing the format of
-voutput.brikis98 commentedon Apr 21, 2018
That would be great 👍
I agree the output is jumbled, but it's actually surprisingly easy to follow so long as each line is prefixed with the test name, and if it's very
grep-friendly to make things even easier to read.agnivade commentedon Apr 22, 2018
Yea, this is totally OK. Usually, I run that single test when debugging a test. So I don't need to bother about other outputs getting jumbled. I just want the log output to appear immediately.
[-]Proposal: add support for streaming logs from t.Log as they happen, rather than buffering until the end of the test[/-][+]proposal: log: stream t.Log output as it happens[/+][-]proposal: log: stream t.Log output as it happens[/-][+]proposal: testing: stream t.Log output as it happens[/+]rsc commentedon Apr 23, 2018
I'm skeptical that we can modify -v output, for what little that's worth.
brikis98 commentedon Apr 26, 2018
One more note on the topic of log streaming. It seems that if you run
go testagainst multiple packages (e.g.,go test -v ./...), the log output is buffered, and usingfmt.Printlnor any other utility that writes tostdoutdoes not help in this instance. I'm not sure on the buffering logic, but it seems like no log output shows up until all the tests in an entire package are done, and the log output is displayed one package at a time.This leads to all the same issues mentioned above, and is especially problematic if you have a single slow package (e.g., one with integration tests), as many CI systems will kill your tests after a certain timeout with no log output (e.g., 10 min in CircleCI). Changing your code doesn't help in this case. The only workaround I've found is to set
-p 1, so each package is tested sequentially. This seems to allowfmt.Printlnto stream tostdoutas normal, but significantly reduces the parallelism you can get in your tests.ianlancetaylor commentedon Apr 30, 2018
We could argue that the output of
-test.vis intended for human consumption, and as such is OK to change along the lines that @randall77 suggests above. Programs that want to parse test output should use-test.json. Of course, this means that we might need to modify the-test.jsonoutput in some way so that thetest2jsonprogram can parse the new output.rsc commentedon May 7, 2018
The redefinition of -v is a bit concerning to me but maybe not enough to sink the idea.
Let's land this early in the Go 1.12 cycle, along with changes to test2json to parse the new prefixes.
If it turns out to be bad, we'll have time to roll it back.
[-]proposal: testing: stream t.Log output as it happens[/-][+]testing: stream t.Log output as it happens[/+]31 remaining items
ianlancetaylor commentedon Nov 26, 2019
Using an optional flag would just be another knob, and it shouldn't be necessary. People who want something like the old output should be able to run the current output through sed. (I'm not saying the current output is clearly better, I'm just saying that I don't think another knob is the right approach.)
I don't think the testing package will ever display
t.Logoutput between result lines, but I believe that it can display data written to standard output between result lines.cybrcodr commentedon Nov 26, 2019
Thanks for the response.
It won't be a simple sed to do exact match for all cases:)
Having standard out between result lines can mess up parsing if the output produces fake result lines. Anyways, it's probably not likely.
gopherbot commentedon Feb 15, 2020
Change https://golang.org/cl/219540 mentions this issue:
testing: remove obsolete comment in testing.(*T) docstesting: remove obsolete comment in testing.(*T) docs
gopherbot commentedon Mar 31, 2020
Change https://golang.org/cl/226757 mentions this issue:
test2json: associate streaming log output with the originating testianlancetaylor commentedon Apr 17, 2020
See #38458, which suggests changing the format again.
gopherbot commentedon May 4, 2020
Change https://golang.org/cl/232157 mentions this issue:
testing: reformat test chatty outputgopherbot commentedon May 4, 2020
Change https://golang.org/cl/229085 mentions this issue:
testing: reformat test chatty outputtesting: reformat test chatty output
gopherbot commentedon Jul 9, 2020
Change https://golang.org/cl/241660 mentions this issue:
doc/go1.15: add line for testing streaming changedoc/go1.15: add line for testing streaming change
gopherbot commentedon Jul 10, 2020
Change https://golang.org/cl/242057 mentions this issue:
[release-branch.go1.14] testing: reformat test chatty output[release-branch.go1.14] testing: reformat test chatty output