From 3f3885a2b0f17e93a88c0bc8c1754197042bf533 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Wed, 1 Mar 2023 07:30:37 -0700 Subject: [PATCH] fix(agent): Only rotate log on SIGHUP if needed (#12740) --- internal/rotate/file_writer.go | 7 ++++++- internal/rotate/file_writer_test.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/rotate/file_writer.go b/internal/rotate/file_writer.go index ddbe3bbe96fb3..797560358c2cc 100644 --- a/internal/rotate/file_writer.go +++ b/internal/rotate/file_writer.go @@ -95,7 +95,12 @@ func (w *FileWriter) Close() (err error) { defer w.Unlock() // Rotate before closing - if err = w.rotate(); err != nil { + if err := w.rotateIfNeeded(); err != nil { + return err + } + + // Close the file if we did not rotate + if err := w.current.Close(); err != nil { return err } diff --git a/internal/rotate/file_writer_test.go b/internal/rotate/file_writer_test.go index b24ece5291c79..b584671aa2a1f 100644 --- a/internal/rotate/file_writer_test.go +++ b/internal/rotate/file_writer_test.go @@ -129,7 +129,7 @@ func TestFileWriter_DeleteArchives(t *testing.T) { } } -func TestFileWriter_CloseRotates(t *testing.T) { +func TestFileWriter_CloseDoesNotRotate(t *testing.T) { tempDir := t.TempDir() maxSize := int64(9) writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), 0, maxSize, -1) @@ -138,5 +138,5 @@ func TestFileWriter_CloseRotates(t *testing.T) { files, _ := os.ReadDir(tempDir) assert.Equal(t, 1, len(files)) - assert.Regexp(t, "^test\\.[^\\.]+\\.log$", files[0].Name()) + assert.Regexp(t, "^test.log$", files[0].Name()) }