From a579357343682236627b6451141d831682cbf5d5 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Sun, 27 Sep 2026 15:05:01 +0200 Subject: [PATCH] refactor(logger): choose the console backend with build tags The runtime.GOOS switch compiled the syslog branch into Windows builds, where go-logging's syslog stub always returns an error. staticcheck therefore reported SA4023 at logger.go:95 on every Windows lint run, keeping `make lint-go` red on a clean main there while Linux CI never saw it. console_windows.go and console_other.go now pick the backend at build time, with each platform's behaviour unchanged. No test can observe build-tag selection: `golangci-lint run` on Windows goes from 1 issue to 0, and `GOOS=linux golangci-lint run ./internal/logger/...` stays clean. --- internal/logger/console_other.go | 20 ++++++++++++++++++++ internal/logger/console_windows.go | 14 ++++++++++++++ internal/logger/logger.go | 24 ++---------------------- 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 internal/logger/console_other.go create mode 100644 internal/logger/console_windows.go diff --git a/internal/logger/console_other.go b/internal/logger/console_other.go new file mode 100644 index 000000000..aa344a83d --- /dev/null +++ b/internal/logger/console_other.go @@ -0,0 +1,20 @@ +//go:build !windows + +package logger + +import ( + "fmt" + "os" + + "github.com/op/go-logging" +) + +// newConsoleBackend prefers syslog and falls back to stderr when it is unavailable. +func newConsoleBackend() (backend logging.Backend, includeTime bool) { + syslogBackend, err := logging.NewSyslogBackend("") + if err == nil { + return syslogBackend, false + } + fmt.Fprintf(os.Stderr, "syslog backend disabled: %v\n", err) + return logging.NewLogBackend(os.Stderr, "", 0), os.Getppid() > 0 +} diff --git a/internal/logger/console_windows.go b/internal/logger/console_windows.go new file mode 100644 index 000000000..b758859d6 --- /dev/null +++ b/internal/logger/console_windows.go @@ -0,0 +1,14 @@ +//go:build windows + +package logger + +import ( + "os" + + "github.com/op/go-logging" +) + +// newConsoleBackend logs to stderr: go-logging has no syslog on Windows. +func newConsoleBackend() (backend logging.Backend, includeTime bool) { + return logging.NewLogBackend(os.Stderr, "", 0), true +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go index e428cad96..b2f5c6c53 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path/filepath" - "runtime" "sync" "sync/atomic" "time" @@ -79,28 +78,9 @@ func InitLogger(level logging.Level) { logger.Store(newLogger) } -// initDefaultBackend creates the console/syslog logging backend. -// Windows: Uses stderr directly (no syslog support) -// Unix-like: Attempts syslog, falls back to stderr +// initDefaultBackend creates the console logging backend: syslog where the platform has it, else stderr. func initDefaultBackend() logging.Backend { - var backend logging.Backend - includeTime := false - - if runtime.GOOS == "windows" { - // Windows: Use stderr directly (no syslog support) - backend = logging.NewLogBackend(os.Stderr, "", 0) - includeTime = true - } else { - // Unix-like: Try syslog, fallback to stderr - if syslogBackend, err := logging.NewSyslogBackend(""); err != nil { - fmt.Fprintf(os.Stderr, "syslog backend disabled: %v\n", err) - backend = logging.NewLogBackend(os.Stderr, "", 0) - includeTime = os.Getppid() > 0 - } else { - backend = syslogBackend - } - } - + backend, includeTime := newConsoleBackend() return logging.NewBackendFormatter(backend, newFormatter(includeTime)) }