Skip to content

testing: allow changing caller depth used by T.Logf #39121

@sylr

Description

@sylr

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

$ go version
go1.14.3

Does this issue reproduce with the latest release?

Yes

What did you do?

I made a lib which requires a logger struct of the following interface:

type Logger interface {
	Tracef(string, ...interface{})
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
}

For testing I created the following logger:

type testLogger struct {
	*testing.T
}

func (t *testLogger) Tracef(format string, vals ...interface{}) {
	t.Logf("go-libqd/config: "+format, vals...)
}

func (t *testLogger) Debugf(format string, vals ...interface{}) {
	t.Logf("go-libqd/config: "+format, vals...)
}

func (t *testLogger) Infof(format string, vals ...interface{}) {
	t.Logf("go-libqd/config: "+format, vals...)
}

func (t *testLogger) Warnf(format string, vals ...interface{}) {
	t.Logf("go-libqd/config: "+format, vals...)
}

What did you expect to see?

I would like the ability to control the depth that the T.Logf function uses to display the log caller so that wrapping T.Logf() still outputs correct file names and log lines.

type testLogger struct {
	*testing.T
}

func (t *testLogger) Tracef(format string, vals ...interface{}) {
	t.LogfDepth("go-libqd/config: "+format, 4, vals...)
}
...

What did you see instead?

The file names and log lines are wrong due to the wrapping made arround T.Logf

=== RUN   TestMyConfig
    TestMyConfig: config_test.go:61: go-libqd/config: Watching config files /var/folders/z0/rcywz4nn3jg6g96h67c_4xzdw31lvl/T/libqd-config-617079976
    TestMyConfig: config_test.go:197: &config.MyConfig{File:"/var/folders/z0/rcywz4nn3jg6g96h67c_4xzdw31lvl/T/libqd-config-617079976", Verbose:[]bool{true, true, true, true, true, true}}
    TestMyConfig: config_test.go:61: go-libqd/config: fsnotify: /var/folders/z0/rcywz4nn3jg6g96h67c_4xzdw31lvl/T/libqd-config-617079976 -> CHMOD
    TestMyConfig: config_test.go:61: go-libqd/config: fsnotify: /var/folders/z0/rcywz4nn3jg6g96h67c_4xzdw31lvl/T/libqd-config-617079976 -> WRITE
    TestMyConfig: config_test.go:61: go-libqd/config: Config file changed
    TestMyConfig: config_test.go:65: go-libqd/config: Reloading config
    TestMyConfig: config_test.go:57: go-libqd/config: Signaling new conf 0xc000092a80 in chan 0xc00018c060
    TestMyConfig: config_test.go:218: &config.MyConfig{File:"/var/folders/z0/rcywz4nn3jg6g96h67c_4xzdw31lvl/T/libqd-config-617079976", Verbose:[]bool{true, true, true, true, true, true}}
--- PASS: TestMyConfig (0.00s)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions