From eec844d85059574dbddde2edc244ea0f7c6fbb41 Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 21 Dec 2025 18:57:27 +0800 Subject: [PATCH] feat: add millisecond-precision timestamps to all log entries (#91) - Add timestamp prefix format [YYYY-MM-DD HH:MM:SS.mmm] to every log entry - Resolves issue where logs lacked time information, making it impossible to determine when events (like "Unknown event format" errors) occurred - Update tests to handle new timestamp format by stripping prefixes during validation - All 27+ tests pass with new format Implementation: - Modified logger.go:369-370 to inject timestamp before message - Updated concurrent_stress_test.go to strip timestamps for format checks Fixes #81 Generated with SWE-Agent.ai Co-authored-by: SWE-Agent.ai --- codeagent-wrapper/concurrent_stress_test.go | 15 +++++++++++++-- codeagent-wrapper/logger.go | 3 ++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/codeagent-wrapper/concurrent_stress_test.go b/codeagent-wrapper/concurrent_stress_test.go index 0b376db..822289a 100644 --- a/codeagent-wrapper/concurrent_stress_test.go +++ b/codeagent-wrapper/concurrent_stress_test.go @@ -13,6 +13,16 @@ import ( "time" ) +func stripTimestampPrefix(line string) string { + if !strings.HasPrefix(line, "[") { + return line + } + if idx := strings.Index(line, "] "); idx >= 0 { + return line[idx+2:] + } + return line +} + // TestConcurrentStressLogger 高并发压力测试 func TestConcurrentStressLogger(t *testing.T) { if testing.Short() { @@ -79,7 +89,8 @@ func TestConcurrentStressLogger(t *testing.T) { // 验证日志格式(纯文本,无前缀) formatRE := regexp.MustCompile(`^goroutine-\d+-msg-\d+$`) for i, line := range lines[:min(10, len(lines))] { - if !formatRE.MatchString(line) { + msg := stripTimestampPrefix(line) + if !formatRE.MatchString(msg) { t.Errorf("line %d has invalid format: %s", i, line) } } @@ -291,7 +302,7 @@ func TestLoggerOrderPreservation(t *testing.T) { sequences := make(map[int][]int) // goroutine ID -> sequence numbers for scanner.Scan() { - line := scanner.Text() + line := stripTimestampPrefix(scanner.Text()) var gid, seq int // Parse format: G0-SEQ0001 (without INFO: prefix) _, err := fmt.Sscanf(line, "G%d-SEQ%04d", &gid, &seq) diff --git a/codeagent-wrapper/logger.go b/codeagent-wrapper/logger.go index cbe338a..425cfc4 100644 --- a/codeagent-wrapper/logger.go +++ b/codeagent-wrapper/logger.go @@ -366,7 +366,8 @@ func (l *Logger) run() { defer ticker.Stop() writeEntry := func(entry logEntry) { - fmt.Fprintf(l.writer, "%s\n", entry.msg) + timestamp := time.Now().Format("2006-01-02 15:04:05.000") + fmt.Fprintf(l.writer, "[%s] %s\n", timestamp, entry.msg) // Cache error/warn entries in memory for fast extraction if entry.isError {