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>
This commit is contained in:
houyuwushang
2026-09-17 08:50:39 +08:00
committed by GitHub
parent 84a93a0b89
commit b93e449b2f
20 changed files with 1064 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
// Package log implements a small wrapper around the std lib log package. It
// implements log levels by prefixing the logs with [INFO], [DEBUG], [WARNING]
// or [ERROR]. Debug logging is available and enabled if the *debug* plugin is
// used.
// used. Configure can opt into structured JSON output instead of text prefixes.
//
// log.Info("this is some logging"), will log on the Info level.
//
@@ -41,11 +41,19 @@ func (d *d) Value() bool {
// logf calls log.Printf prefixed with level.
func logf(level, format string, v ...any) {
if b := jsonBackend.Load(); b != nil {
b.log(level, "", fmt.Sprintf(format, v...))
return
}
golog.Print(level, fmt.Sprintf(format, v...))
}
// log calls log.Print prefixed with level.
func log(level string, v ...any) {
if b := jsonBackend.Load(); b != nil {
b.log(level, "", fmt.Sprint(v...))
return
}
golog.Print(level, fmt.Sprint(v...))
}
@@ -94,7 +102,7 @@ func Fatal(v ...any) { log(fatal, v...); os.Exit(1) }
func Fatalf(format string, v ...any) { logf(fatal, format, v...); os.Exit(1) }
// Discard sets the log output to /dev/null.
func Discard() { golog.SetOutput(io.Discard) }
func Discard() { SetOutput(io.Discard) }
const (
debug = "[DEBUG] "