Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions internal/logger/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions internal/logger/global_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package logger

import (
"log"
"os"
"sync"
)

Expand Down Expand Up @@ -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)
}
}
6 changes: 1 addition & 5 deletions internal/logger/server_file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Loading