Skip to content

proposal: testing: make Log output configurable #28544

@letientai299

Description

@letientai299

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions