Exemplo n.º 1
0
func TestClose(t *testing.T) {
	var buff bytes.Buffer
	log.SetOutput(&buff)
	log.SetLogLevel(log.LevelAll)
	err := log.Close() // should have no effect, because Buffer is not a WriteCloser
	if err != nil {
		t.Errorf("Close shouldn't have returned error: %s", err)
	}
	log.Info("Hello")
	if !strings.Contains(buff.String(), "Hello") {
		t.Errorf("Close killed output of Buffer: %s", buff.String())
	}
	read, write := io.Pipe()
	log.SetOutput(write)
	go func() { // we need to read before writing to the pipe
		b := make([]byte, 100)
		n, err := read.Read(b)
		if !strings.Contains(string(b), "Hello") {
			t.Errorf("Pipe read failed: %d, %s, %v", n, string(b), err)
		}
		n, err = read.Read(b)
		if n != 0 || err != io.EOF {
			t.Errorf("Close didn't work: %d bytes read, %s (%v)", n, string(b), err)
		}
	}()
	log.Info("Hello")
	err = log.Close()
	if err != nil {
		t.Errorf("Close shouldn't have returned error on pipe: %s", err)
	}
	log.Info("Hello")
}
Exemplo n.º 2
0
func TestLog(t *testing.T) {
	var buff bytes.Buffer
	log.SetOutput(&buff)
	log.SetLogLevel(log.LevelAll ^ log.LevelDebug ^ log.LevelFatal)
	log.Debug("Hello %s", "world") // this won't get written
	log.Info("%s %d", "Hello world", 1234)
	log.Close() // should have no effect, because Buffer is not a WriteCloser
	log.Error("%v", struct{ s string }{"Hello world"})
	log.Fatal("%v", struct{ s string }{"Hello world"}) // won't trigger or write
	log.Custom("TEST", "Hello world", "extra arg")
	// we do Panic later
	b := buff.String()
	if c := strings.Count(b, "Hello"); c != 3 {
		t.Errorf("Bad \"Hello\" count: %d\n%s", c, b)
	}
	if strings.Count(b, "DEBUG") != 0 {
		t.Error("DEBUG shouldn't have been written")
	}
	if strings.Count(b, "INFO") != 1 {
		t.Error("INFO wasn't written")
	}
	if strings.Count(b, "ERROR") != 1 {
		t.Error("ERROR wasn't written")
	}
	if strings.Count(b, "FATAL") != 0 {
		t.Error("FATAL shouldn't have been written")
	}
	if strings.Count(b, "TEST") != 1 {
		t.Error("TEST wasn't written")
	}
	t.Log(b)
	buff.Truncate(0)
	log.SetLogLevel(log.LevelAll)
	defer func() {
		recover()
		if !strings.Contains(buff.String(), "Hello world 1234") {
			t.Errorf("Bad Panic() string: %s", buff.String())
		}
	}()
	log.Panic("%s %d", "Hello world", 1234)
	t.Error("Panic didn't panic")
}