Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: testing: make Log output configurable #28544

letientai299 opened this issue Nov 2, 2018 · 3 comments

proposal: testing: make Log output configurable #28544

letientai299 opened this issue Nov 2, 2018 · 3 comments


Copy link

letientai299 commented Nov 2, 2018

What version of Go are you using (go version)?

$ go version
go version go1.10.4 linux/amd64

Feature request: make t.Log output configurable.

We're using a logging library in main code, and to show application log in test code, we redirect it via following code (in a file named util_test.go).

// a io.WriteCLoser implementation that will direct data into testing.T.Logf(),
// aim to use in test functions to print relevant log entries for each test case.
// The log will only be shown if test fails, or -test.v is used.
type testingLogOutput struct {
	t *testing.T

func (output *testingLogOutput) Close() error { return nil }

func (output *testingLogOutput) Write(p []byte) (n int, err error) {
	s := string(p)
	s = strings.TrimSpace(s)
	return len(p), nil

func newTestLogWriter(t *testing.T) io.WriteCloser {
	return &testingLogOutput{t: t}

It's nice that relevant log entries will be show per for each test case. But t.Log will always print the line number of file where it is called, and for our use case, it's always the same:

util_test.go:139: 2018/11/02 13:44:40.724412 [VERBOSE][ItemDM.go:3405][logid:0x794_0]...
util_test.go:139: 2018/11/02 13:44:40.724437 [VERBOSE][ItemDM.go:1814][logid:0x794_0]...
util_test.go:139: 2018/11/02 13:44:40.724447 [VERBOSE][ItemDM.go:2639][logid:0x794_0]...
util_test.go:139: 2018/11/02 13:44:40.724494 [VERBOSE][ItemDM.go:2931][logid:0x794_0]...
util_test.go:139: 2018/11/02 13:44:40.724499 [DETAIL][ItemDM.go:2933][logid:0x794_0]...

Anyway to customize t.Log output? If it's not built-in yet, do you think it's worth a patch?

@agnivade agnivade changed the title Make t.Log output configurable proposal: testing: make Log output configurable Nov 2, 2018
@gopherbot gopherbot added this to the Proposal milestone Nov 2, 2018
Copy link

ianlancetaylor commented Nov 2, 2018

Can you just use t.Helper?

Copy link
Contributor Author

letientai299 commented Nov 3, 2018

Thanks for the hint. I should have scanned the doc closer. t.Helper() works for our project now. But the proposal remains valid.

Copy link

ianlancetaylor commented Nov 3, 2018

We don't plan to support full customization of t.Log output.

@golang golang locked and limited conversation to collaborators Nov 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

3 participants