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

Closed
letientai299 opened this issue Nov 2, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@letientai299
Copy link

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)
	output.t.Log(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

@gopherbot gopherbot added the Proposal label Nov 2, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 2, 2018

Can you just use t.Helper?

@letientai299

This comment has been minimized.

Copy link
Author

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.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.