2023-03-20 15:08:30 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOutput(t *testing.T) {
|
|
|
|
testOutput := &bytes.Buffer{}
|
|
|
|
logger.SetOutputForTests(testOutput)
|
|
|
|
defer logger.ResetOutputForTest()
|
|
|
|
|
|
|
|
log := &Logger{}
|
|
|
|
|
|
|
|
mustMatch := func(exp string) {
|
|
|
|
t.Helper()
|
2024-07-12 19:57:56 +00:00
|
|
|
|
2023-03-20 15:08:30 +00:00
|
|
|
if exp == "" {
|
|
|
|
if testOutput.String() != "" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected output to be empty; got %q", testOutput.String())
|
2023-03-20 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !strings.Contains(testOutput.String(), exp) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("output %q should contain %q", testOutput.String(), exp)
|
2023-03-20 15:08:30 +00:00
|
|
|
}
|
|
|
|
fmt.Println(testOutput.String())
|
|
|
|
testOutput.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("foo")
|
|
|
|
mustMatch("foo")
|
|
|
|
|
|
|
|
log.Infof("info %d", 2)
|
|
|
|
mustMatch("info 2")
|
|
|
|
|
|
|
|
log.Errorf("error %s %d", "baz", 5)
|
|
|
|
mustMatch("error baz 5")
|
|
|
|
|
|
|
|
log.Suppress(true)
|
|
|
|
|
|
|
|
log.Warnf("foo")
|
|
|
|
mustMatch("")
|
|
|
|
|
|
|
|
log.Infof("info %d", 2)
|
|
|
|
mustMatch("")
|
|
|
|
|
|
|
|
log.Errorf("error %q %d", "baz", 5)
|
|
|
|
mustMatch("")
|
|
|
|
|
|
|
|
}
|