Files
coredns/plugin/pkg/log/log_test.go
houyuwushang b93e449b2f Add opt-in JSON logging with structured DNS query fields (#8553)
* plugin/pkg/log: add opt-in JSON logging backend

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>

* plugin/log: emit typed query records in JSON mode

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>

* coremain: expose process-wide JSON logging

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>

---------

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>
2026-09-16 17:50:39 -07:00

74 lines
1.4 KiB
Go

package log
import (
"bytes"
golog "log"
"strings"
"testing"
)
func TestDebug(t *testing.T) {
var f bytes.Buffer
golog.SetOutput(&f)
// D == false
Debug("debug")
if x := f.String(); x != "" {
t.Errorf("Expected no debug logs, got %s", x)
}
f.Reset()
D.Set()
Debug("debug")
if x := f.String(); !strings.Contains(x, debug+"debug") {
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
}
f.Reset()
D.Clear()
Debug("debug")
if x := f.String(); x != "" {
t.Errorf("Expected no debug logs, got %s", x)
}
}
func TestDebugx(t *testing.T) {
t.Cleanup(D.Clear)
var f bytes.Buffer
golog.SetOutput(&f)
D.Set()
Debugf("%s", "debug")
if x := f.String(); !strings.Contains(x, debug+"debug") {
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
}
f.Reset()
Debug("debug")
if x := f.String(); !strings.Contains(x, debug+"debug") {
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
}
}
func TestLevels(t *testing.T) {
var f bytes.Buffer
const ts = "test"
golog.SetOutput(&f)
Info(ts)
if x := f.String(); !strings.Contains(x, info+ts) {
t.Errorf("Expected log to be %s, got %s", info+ts, x)
}
f.Reset()
Warning(ts)
if x := f.String(); !strings.Contains(x, warning+ts) {
t.Errorf("Expected log to be %s, got %s", warning+ts, x)
}
f.Reset()
Error(ts)
if x := f.String(); !strings.Contains(x, err+ts) {
t.Errorf("Expected log to be %s, got %s", err+ts, x)
}
}