Skip to content

Commit 83944ea

Browse files
authored
🐛 Fix zerolog closing the writer passed to it (#658)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [ ] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent c652d81 commit 83944ea

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

changes/20250729141206.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug: Fix zerolog closing the writer passed to it

utils/logs/writer.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (w *MultipleWritersWithSource) AddWriters(writers ...WriterWithSource) erro
4141
}
4242
return nil
4343
}
44+
4445
func (w *MultipleWritersWithSource) Write(p []byte) (n int, err error) {
4546
writers, err := w.GetWriters()
4647
if err != nil {
@@ -133,12 +134,24 @@ func NewDiodeWriterForSlowWriterWithoutClosing(slowWriter WriterWithSource, ring
133134
return newDiodeWriterForSlowWriter(false, slowWriter, ringBufferSize, pollInterval, droppedMessagesLogger)
134135
}
135136

137+
type nonCloseableWriter struct {
138+
io.Writer
139+
}
140+
141+
func (ncw nonCloseableWriter) Close() error {
142+
return nil
143+
}
144+
136145
func newDiodeWriterForSlowWriter(closeWriterOnClose bool, slowWriter WriterWithSource, ringBufferSize int, pollInterval time.Duration, droppedMessagesLogger Loggers) WriterWithSource {
137146
closerStore := parallelisation.NewCloserStore(false)
138147
if closeWriterOnClose {
139148
closerStore.RegisterCloser(slowWriter)
140149
}
141-
d := diode.NewWriter(slowWriter, ringBufferSize, pollInterval, func(missed int) {
150+
151+
// We pass a wrapper that "overrides" the close method to do nothing,
152+
// this is because we control the writer's close behaviour through the `closeWriterOnClose` check
153+
ncw := nonCloseableWriter{slowWriter}
154+
d := diode.NewWriter(ncw, ringBufferSize, pollInterval, func(missed int) {
142155
if droppedMessagesLogger != nil {
143156
droppedMessagesLogger.LogError(fmt.Sprintf("Logger dropped %d messages", missed))
144157
}

0 commit comments

Comments
 (0)