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
Closed

proposal: testing: implement io.Writer with T and B #28778

poy opened this issue Nov 13, 2018 · 4 comments

Comments

@poy
Copy link
Contributor

poy 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
Copy link
Contributor

dpinela 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
Copy link
Contributor Author

poy 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
Copy link
Member

SamWhited commented Nov 14, 2018

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

@rsc
Copy link
Contributor

rsc 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 as completed Nov 28, 2018
@golang golang locked and limited conversation to collaborators Nov 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants