From 6264d56d88ae1cfa2b0842ee0d6a486f1e059dad Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 13 Nov 2024 02:34:09 +0000 Subject: [PATCH 1/2] remove max file size check in openExistingOrNew Signed-off-by: Arjun Raja Yogidas --- logrotate.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/logrotate.go b/logrotate.go index 9cac980..4cd6738 100644 --- a/logrotate.go +++ b/logrotate.go @@ -136,7 +136,7 @@ func (l *Logger) Write(p []byte) (n int, err error) { } if l.file == nil { - if err = l.openExistingOrNew(len(p)); err != nil { + if err = l.openExistingOrNew(); err != nil { return 0, err } } @@ -272,7 +272,7 @@ func (l *Logger) backupName(name, nameTimeFormat string, local bool) (string, er // openExistingOrNew opens the logfile if it exists and if the current write // would not put it over MaxBytes. If there is no such file or the write would // put it over the MaxBytes, a new file is created. -func (l *Logger) openExistingOrNew(writeLen int) error { +func (l *Logger) openExistingOrNew() error { l.millRun() filename := l.filename() @@ -284,10 +284,6 @@ func (l *Logger) openExistingOrNew(writeLen int) error { return fmt.Errorf("error getting log file info: %s", err) } - if info.Size()+int64(writeLen) >= l.max(int64(writeLen)) { - return l.rotate() - } - file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644) if err != nil { // if we fail to open the old log file for some reason, just ignore From 7ec92c8f53505bc37825b593cdcc7e8833b6904a Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 19 Nov 2024 00:40:06 +0000 Subject: [PATCH 2/2] add openExistingOrNew unit test Signed-off-by: Arjun Raja Yogidas --- logrotate.go | 5 ++--- logrotate_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/logrotate.go b/logrotate.go index 4cd6738..d8b9cb7 100644 --- a/logrotate.go +++ b/logrotate.go @@ -269,9 +269,8 @@ func (l *Logger) backupName(name, nameTimeFormat string, local bool) (string, er return filepath.Join(dir, filename), nil } -// openExistingOrNew opens the logfile if it exists and if the current write -// would not put it over MaxBytes. If there is no such file or the write would -// put it over the MaxBytes, a new file is created. +// openExistingOrNew opens the logfile if it exists. +// If there is no such file a new file is created. func (l *Logger) openExistingOrNew() error { l.millRun() diff --git a/logrotate_test.go b/logrotate_test.go index 2cb0b03..9e8bd53 100644 --- a/logrotate_test.go +++ b/logrotate_test.go @@ -78,6 +78,44 @@ func TestOpenExisting(t *testing.T) { fileCount(dir, 1, t) } +func TestOpenExistingOrNew(t *testing.T) { + dir := makeTempDir("TestOpenExistingOrNew", t) + defer os.RemoveAll(dir) + + filename := logFile(dir) + l := &Logger{ + Filename: filename, + MaxBytes: 100, + } + + // File doesn't exist + err := l.openExistingOrNew() + isNil(err, t) + assert(l.file != nil, t, "Expected file to be opened") + equals(int64(0), l.size, t) + + content := []byte("Hello, World!") + _, err = l.file.Write(content) + isNil(err, t) + l.size = int64(len(content)) + + // Close + err = l.file.Close() + isNil(err, t) + l.file = nil + + // File exists and is not over MaxBytes + err = l.openExistingOrNew() + isNil(err, t) + assert(l.file != nil, t, "Expected file to be opened") + equals(int64(len(content)), l.size, t) + + // Close + err = l.file.Close() + isNil(err, t) + l.file = nil +} + func TestWriteTooLong(t *testing.T) { currentTime = fakeTime dir := makeTempDir("TestWriteTooLong", t)