Skip to content
Closed
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
22 changes: 20 additions & 2 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ var (

type Logger struct {
file *os.File

// writeMu serializes file writes.
// Although POSIX systems typically guarantee atomicity
// for single write() syscalls on regular files,
// Windows and some filesystems may not.
//
// This prevents log line interleaving across platforms.
writeMu sync.Mutex
}

type LogEntry struct {
Expand Down Expand Up @@ -116,13 +124,23 @@ func logMessage(level LogLevel, component string, message string, fields map[str
}
}

if logger.file != nil {
mu.RLock()
fileptr := logger.file

if fileptr != nil {
jsonData, err := json.Marshal(entry)
if err == nil {
logger.file.Write(append(jsonData, '\n'))
logger.writeMu.Lock()
_, err = fileptr.Write(append(jsonData, '\n'))
logger.writeMu.Unlock()
if err != nil {
log.Println("Failed to write to file:", err.Error())
}
}
}

mu.RUnlock()

var fieldStr string
if len(fields) > 0 {
fieldStr = " " + formatFields(fields)
Expand Down
33 changes: 33 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package logger

import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)

Expand Down Expand Up @@ -137,3 +142,31 @@ func TestLoggerHelperFunctions(t *testing.T) {
DebugC("test", "Debug with component")
WarnF("Warning with fields", map[string]any{"key": "value"})
}

// TestConcurrentFileLogging validates concurrent file writes are race-safe and complete.
// Run: go test -race ./pkg/logger -run TestConcurrentFileLogging -count=1
func TestConcurrentFileLogging(t *testing.T) {
dir := t.TempDir()
logFile := filepath.Join(dir, "test.log")
if err := EnableFileLogging(logFile); err != nil {
t.Fatal(err)
}
defer DisableFileLogging()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
InfoCF("test", fmt.Sprintf("msg-%d", n), map[string]any{"n": n})
}(i)
}
wg.Wait()
data, err := os.ReadFile(logFile)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
if len(lines) != 100 {
t.Errorf("expected 100 log lines, got %d", len(lines))
}
}