feat: add INFO, WARNING, ERROR logging for Unix socket output
Some checks failed
Build RPM Package / Build RPM Packages (CentOS 7, Rocky 8/9/10) (push) Has been cancelled
Some checks failed
Build RPM Package / Build RPM Packages (CentOS 7, Rocky 8/9/10) (push) Has been cancelled
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@ -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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user