From 2c8edf050a9e725d41731aa0c15e2d1f604b2e2f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 22:31:01 +0000 Subject: [PATCH 1/2] Initial plan From 4b8ba35ba3c09c485d271a56d8a4740571ed8b6d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 22:47:55 +0000 Subject: [PATCH 2/2] refactor(logger): extract syncFileWithWarning helper to eliminate duplicate sync boilerplate --- internal/logger/file_logger.go | 8 ++------ internal/logger/global_helpers.go | 14 ++++++++++++++ internal/logger/server_file_logger.go | 6 +----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/internal/logger/file_logger.go b/internal/logger/file_logger.go index 6b6b40a2..ed5869c1 100644 --- a/internal/logger/file_logger.go +++ b/internal/logger/file_logger.go @@ -87,12 +87,8 @@ func (fl *FileLogger) Log(level LogLevel, category, format string, args ...inter fl.logger.Println(logLine) // Flush the log to disk immediately to ensure it's readable by other processes - if fl.logFile != nil { - if err := fl.logFile.Sync(); err != nil { - // Log sync errors to stderr to avoid infinite recursion - log.Printf("WARNING: Failed to sync log file: %v", err) - } - } + // Log sync errors to stderr to avoid infinite recursion + syncFileWithWarning(fl.logFile, "") } // GetWriter returns the underlying io.Writer for the file logger diff --git a/internal/logger/global_helpers.go b/internal/logger/global_helpers.go index 2734e5af..21124587 100644 --- a/internal/logger/global_helpers.go +++ b/internal/logger/global_helpers.go @@ -18,6 +18,7 @@ package logger import ( "log" + "os" "sync" ) @@ -207,3 +208,16 @@ func strictLoggerOnInitError[T any](err error) (T, error) { var zero T return zero, err } + +// syncFileWithWarning flushes file to disk and logs a WARNING to stderr if the +// sync fails. context is appended to the log message (e.g. " for server github") +// to distinguish call sites; pass an empty string for the main log file. +// A nil file is silently ignored so callers need not check before calling. +func syncFileWithWarning(file *os.File, context string) { + if file == nil { + return + } + if err := file.Sync(); err != nil { + log.Printf("WARNING: Failed to sync log file%s: %v", context, err) + } +} diff --git a/internal/logger/server_file_logger.go b/internal/logger/server_file_logger.go index fb7778b7..fcd6a205 100644 --- a/internal/logger/server_file_logger.go +++ b/internal/logger/server_file_logger.go @@ -93,11 +93,7 @@ func (sfl *ServerFileLogger) Log(serverID string, level LogLevel, category, form // Flush to disk immediately sfl.mu.RLock() - if file, exists := sfl.files[serverID]; exists { - if err := file.Sync(); err != nil { - log.Printf("WARNING: Failed to sync log file for server %s: %v", serverID, err) - } - } + syncFileWithWarning(sfl.files[serverID], " for server "+serverID) sfl.mu.RUnlock() }