Skip to content

Instantly share code, notes, and snippets.

@aybabtme
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aybabtme/f21f7d29d421e15134c0 to your computer and use it in GitHub Desktop.
Save aybabtme/f21f7d29d421e15134c0 to your computer and use it in GitHub Desktop.
test logger
// io.Writer implementer
type writer func(p []byte) (int, error)
func (w writer) Write(p []byte) (int, error) { return w(p) }
// magic, a testing.T writer!
func testwriter(t *testing.T) io.Writer {
return writer(func(p []byte) (int, error) {
t.Log(string(p))
return 0, nil
})
}
// magic, a testing.T logger!
func testlogger(t *testing.T) *log.Logger {
return log.New(io.MultiWriter(testwriter(t), os.Stderr), "[test]", 0)
}
func TestSomething(t *testing.T) {
log.SetOutput(testwriter(t))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment