如何使用Go日志记录包glog测试代码?

I have implemented a type wrapping glog so that I can add a prefix to log message identifying the emitter of the log in my program and I can change the log level per emitter.

How could I implement the unit tests ? The problem is that glog outputs text to stdErr.

The code is trivial but I would like the have the unit test and 100% coverage like the rest of the code. This programming effort already payed.

Test which captures stderr:

package main

import (
    "bytes"
    "io"
    "os"
    "testing"

    "github.com/golang/glog"
    "strings"
)

func captureStderr(f func()) (string, error) {
    old := os.Stderr // keep backup of the real stderr
    r, w, err := os.Pipe()
    if err != nil {
        return "", err
    }
    os.Stderr = w

    outC := make(chan string)
    // copy the output in a separate goroutine so printing can't block indefinitely
    go func() {
        var buf bytes.Buffer
        io.Copy(&buf, r)
        outC <- buf.String()
    }()

    // calling function which stderr we are going to capture:
    f()

    // back to normal state
    w.Close()
    os.Stderr = old // restoring the real stderr
    return <-outC, nil
}

func TestGlogError(t *testing.T) {
    stdErr, err := captureStderr(func() {
        glog.Error("Test error")
    })
    if err != nil {
        t.Errorf("should not be error, instead: %+v", err)
    }
    if !strings.HasSuffix(strings.TrimSpace(stdErr), "Test error") {
        t.Errorf("stderr should end by 'Test error' but it doesn't: %s", stdErr)
    }
}

running test:

go test -v
=== RUN   TestGlogError
--- PASS: TestGlogError (0.00s)
PASS
ok      command-line-arguments  0.007s

Write an interface that describes your usage. This won't be very pretty if you use the V method, but you have a wrapper so you've already done the hard work that fixing that would entail.

For each package you need to test, define

type Logger interface {
    Infoln(...interface{}) // the methods you actually use in this package
}

And then you can easily swap it out by not referring to glog types directly in your code.