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: implement io.Writer with T and B #28778

Closed
poy opened this issue Nov 13, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@poy
Copy link
Contributor

commented Nov 13, 2018

Problem

Tests that have to create a *log.Logger do not (at least not out of the box) get to take advantage of testing's T.Log and the nice features that come with it.

This implies test writers have to either wrap T.Log up to make it implement io.Writer somehow and pass that into log.New(tlogger, "", 0), or discard their logs all together (e.g., log.New(ioutil.Discard, "", 0)).

Solution

Have testing.T and testing.B implement io.Writer.

This will allow developers to simply create a log.Logger by doing log.New(t, "", 0). It would also be compatible with most other logging libraries.

@dpinela

This comment has been minimized.

Copy link
Contributor

commented Nov 13, 2018

I usually solve this the other way round:

type Logger interface {
  Log(args ...interface{})
}

type stdLogger struct {
  *log.Logger
}

func (s stdLogger) Log(args ...interface{}) { s.Println(args...) }

Then I log to the Logger interface, rather than to the *log.Logger directly.

@poy

This comment has been minimized.

Copy link
Contributor Author

commented Nov 13, 2018

@dpinela That works as long as the developer has control over the interface. If however they are using a dependency that requires a *log.Logger, then that won't work for them.

@SamWhited

This comment has been minimized.

Copy link
Member

commented Nov 14, 2018

For context, see the previous (rejected) proposal at https://golang.org/issues/22513 (#22513).

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 28, 2018

Line-at-a-time logging is not the same as a Write method (Writes are not guaranteed to be aligned with any useful boundary, certainly not lines). But this is not about Write at all. It's about log.Logger, which is/was #22513. Closing as duplicate.

@rsc rsc closed this Nov 28, 2018

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.