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

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

Closed
sylr opened this issue May 17, 2020 · 2 comments
Closed

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

sylr opened this issue May 17, 2020 · 2 comments

Comments

@sylr
Copy link

@sylr sylr commented May 17, 2020

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)
@sylr sylr changed the title testing: allow changing caller depth use by T.Logf testing: allow changing caller depth used by T.Logf May 17, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 17, 2020

Seems like this is what t.Helper is for.

@sylr
Copy link
Author

@sylr sylr commented May 17, 2020

It is. Thanks 👍

@sylr sylr closed this May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.