mirror of
https://github.com/coredns/coredns.git
synced 2026-10-08 19:45:21 -04:00
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:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
|
||||
"go.uber.org/automaxprocs/maxprocs"
|
||||
)
|
||||
@@ -29,6 +30,7 @@ func init() {
|
||||
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
|
||||
flag.BoolVar(&version, "version", false, "Show version")
|
||||
flag.BoolVar(&dnsserver.Quiet, "quiet", false, "Quiet mode (no initialization output)")
|
||||
flag.StringVar(&logFormat, "log-format", "text", "Log format (text or json)")
|
||||
|
||||
caddy.RegisterCaddyfileLoader("flag", caddy.LoaderFunc(confLoader))
|
||||
caddy.SetDefaultCaddyfileLoader("default", caddy.LoaderFunc(defaultLoader))
|
||||
@@ -44,13 +46,20 @@ func init() {
|
||||
func Run() {
|
||||
caddy.TrapSignals()
|
||||
flag.Parse()
|
||||
if logFormat != "text" {
|
||||
if err := clog.Configure(logFormat, os.Stdout); err != nil {
|
||||
mustLogFatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(flag.Args()) > 0 {
|
||||
mustLogFatal(fmt.Errorf("extra command line arguments: %s", flag.Args()))
|
||||
}
|
||||
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(LogFlags)
|
||||
clog.SetOutput(os.Stdout)
|
||||
if !clog.IsJSON() {
|
||||
log.SetFlags(LogFlags)
|
||||
}
|
||||
|
||||
if version {
|
||||
showVersion()
|
||||
@@ -63,7 +72,7 @@ func Run() {
|
||||
|
||||
_, err := maxprocs.Set(maxprocs.Logger(log.Printf))
|
||||
if err != nil {
|
||||
log.Println("[WARNING] Failed to set GOMAXPROCS:", err)
|
||||
clog.Warningf("Failed to set GOMAXPROCS: %v", err)
|
||||
}
|
||||
|
||||
// Get Corefile input
|
||||
@@ -79,7 +88,14 @@ func Run() {
|
||||
}
|
||||
|
||||
if !dnsserver.Quiet {
|
||||
showVersion()
|
||||
if clog.IsJSON() {
|
||||
clog.Info(strings.TrimSuffix(versionString()+releaseString(), "\n"))
|
||||
if devBuild && gitShortStat != "" {
|
||||
clog.Infof("%s\n%s", gitShortStat, gitFilesModified)
|
||||
}
|
||||
} else {
|
||||
showVersion()
|
||||
}
|
||||
}
|
||||
|
||||
// Twiddle your thumbs
|
||||
@@ -94,7 +110,10 @@ func Run() {
|
||||
// log and exits.
|
||||
func mustLogFatal(args ...any) {
|
||||
if !caddy.IsUpgrade() {
|
||||
log.SetOutput(os.Stderr)
|
||||
clog.SetOutput(os.Stderr)
|
||||
}
|
||||
if clog.IsJSON() {
|
||||
clog.Fatal(args...)
|
||||
}
|
||||
log.Fatal(args...)
|
||||
}
|
||||
@@ -176,9 +195,10 @@ func setVersion() {
|
||||
|
||||
// Flags that control program flow or startup
|
||||
var (
|
||||
conf string
|
||||
version bool
|
||||
plugins bool
|
||||
conf string
|
||||
version bool
|
||||
plugins bool
|
||||
logFormat string
|
||||
|
||||
// LogFlags are initially set to 0 for no extra output
|
||||
LogFlags int
|
||||
|
||||
Reference in New Issue
Block a user