Skip to content

Commit 0bc7b22

Browse files
committed
fix: prevent data loss in BracketWriter on write errors
Move buffer advancement after the successful write to the underlying writer. Previously, the line was removed from the buffer before the write, so a failed write (e.g., full disk) would lose the log entry. Also avoids an unnecessary string allocation by passing the byte slice directly to fmt.Fprintf.
1 parent a1da825 commit 0bc7b22

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pkg/logging/logging.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ func (bw *BracketWriter) Write(p []byte) (int, error) {
114114
if idx < 0 {
115115
break
116116
}
117-
line := string(bw.buf[:idx])
118-
bw.buf = append(bw.buf[:0], bw.buf[idx+1:]...)
117+
line := bw.buf[:idx]
119118

120119
ts := time.Now().UTC().Format("2006-01-02T15:04:05.000000000Z")
121120
if _, err := fmt.Fprintf(bw.w, "[%s] %s\n", ts, line); err != nil {
122121
return n, err
123122
}
123+
// Advance the buffer only after a successful write to
124+
// avoid losing the line on transient I/O errors.
125+
bw.buf = append(bw.buf[:0], bw.buf[idx+1:]...)
124126
}
125127
return n, nil
126128
}

0 commit comments

Comments
 (0)