2023-03-20 16:08:30 +01: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 21:57:56 +02:00
|
|
|
|
2023-03-20 16:08:30 +01:00
|
|
|
if exp == "" {
|
|
|
|
if testOutput.String() != "" {
|
2024-07-12 21:57:56 +02:00
|
|
|
t.Fatalf("expected output to be empty; got %q", testOutput.String())
|
2023-03-20 16:08:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !strings.Contains(testOutput.String(), exp) {
|
2024-07-12 21:57:56 +02:00
|
|
|
t.Fatalf("output %q should contain %q", testOutput.String(), exp)
|
2023-03-20 16:08:30 +01: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("")
|
|
|
|
|
|
|
|
}
|