From 2a39f76ecd6587073752bf87a7fdec7b4658b979 Mon Sep 17 00:00:00 2001 From: Jacquin Antoine Date: Sun, 1 Mar 2026 02:23:05 +0100 Subject: [PATCH] feat: add INFO, WARNING, ERROR logging for Unix socket output Co-authored-by: Qwen-Coder --- internal/output/writers.go | 54 ++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/internal/output/writers.go b/internal/output/writers.go index c98f886..f353fa3 100644 --- a/internal/output/writers.go +++ b/internal/output/writers.go @@ -236,12 +236,20 @@ func NewUnixSocketWriterWithConfigAndDebug(socketPath string, dialTimeout, write conn, err := net.DialTimeout("unix", socketPath, w.dialTimeout) if err == nil { w.conn = conn - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: connected to %s", socketPath) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: connected to %s", socketPath) + } else { + w.logger.Printf("[INFO] UnixSocketWriter: connected to %s", socketPath) + } } } else { - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: initial connection to %s failed: %v (will retry on write)", socketPath, err) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: initial connection to %s failed: %v (will retry on write)", socketPath, err) + } else { + w.logger.Printf("[WARNING] UnixSocketWriter: initial connection to %s failed: %v (will retry on write)", socketPath, err) + } } } @@ -273,8 +281,14 @@ func (w *UnixSocketWriter) processQueue() { } w.pendingMu.Unlock() - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: write failed to %s: %v (failures: %d)", w.socketPath, err, consecutiveFailures) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: write failed to %s: %v (failures: %d)", w.socketPath, err, consecutiveFailures) + } else if consecutiveFailures >= w.maxReconnects { + w.logger.Printf("[ERROR] UnixSocketWriter: max reconnection attempts reached for %s (failures: %d)", w.socketPath, consecutiveFailures) + } else if consecutiveFailures > 1 { + w.logger.Printf("[WARNING] UnixSocketWriter: write failed to %s: %v (attempt %d/%d)", w.socketPath, err, consecutiveFailures, w.maxReconnects) + } } // Exponential backoff @@ -336,15 +350,19 @@ func (w *UnixSocketWriter) writeWithReconnect(data []byte) error { return fmt.Errorf("failed to connect to socket %s: %w", w.socketPath, err) } w.conn = conn - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: reconnected to %s", w.socketPath) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: reconnected to %s", w.socketPath) + } else { + w.logger.Printf("[INFO] UnixSocketWriter: connected to %s", w.socketPath) + } } return nil } if err := ensureConn(); err != nil { - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: connection failed to %s: %v", w.socketPath, err) + if w.logger != nil { + w.logger.Printf("[ERROR] UnixSocketWriter: connection failed to %s: %v", w.socketPath, err) } return err } @@ -358,8 +376,12 @@ func (w *UnixSocketWriter) writeWithReconnect(data []byte) error { } // Connection failed, try to reconnect - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: write failed, attempting reconnect to %s", w.socketPath) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: write failed, attempting reconnect to %s", w.socketPath) + } else { + w.logger.Printf("[WARNING] UnixSocketWriter: write failed, attempting reconnect to %s", w.socketPath) + } } _ = w.conn.Close() w.conn = nil @@ -419,8 +441,12 @@ func (w *UnixSocketWriter) Close() error { w.isClosed = true if w.conn != nil { - if w.debug && w.logger != nil { - w.logger.Printf("[DEBUG] UnixSocketWriter: closing connection to %s", w.socketPath) + if w.logger != nil { + if w.debug { + w.logger.Printf("[DEBUG] UnixSocketWriter: closing connection to %s", w.socketPath) + } else { + w.logger.Printf("[INFO] UnixSocketWriter: closing connection to %s", w.socketPath) + } } w.conn.Close() w.conn = nil