From 4222fd97bc5d75f2a9b4c48f20b9fdf69de10a04 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 24 Sep 2023 22:28:03 +0800 Subject: [PATCH] chore: add test for logging rotate size (#3587) --- core/logx/rotatelogger.go | 1 + core/logx/rotatelogger_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go index 58a609d1..4b7f31e7 100644 --- a/core/logx/rotatelogger.go +++ b/core/logx/rotatelogger.go @@ -298,6 +298,7 @@ func (l *RotateLogger) initialize() error { if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil { return err } + l.currentSize = fileInfo.Size() } diff --git a/core/logx/rotatelogger_test.go b/core/logx/rotatelogger_test.go index ff0fdea1..f370aa8c 100644 --- a/core/logx/rotatelogger_test.go +++ b/core/logx/rotatelogger_test.go @@ -517,6 +517,21 @@ func TestGzipFile(t *testing.T) { }) } +func TestRotateLogger_WithExistingFile(t *testing.T) { + const body = "foo" + filename, err := fs.TempFilenameWithText(body) + assert.Nil(t, err) + if len(filename) > 0 { + defer os.Remove(filename) + } + + rule := NewSizeLimitRotateRule(filename, "-", 1, 100, 3, false) + logger, err := NewLogger(filename, rule, false) + assert.Nil(t, err) + assert.Equal(t, int64(len(body)), logger.currentSize) + assert.Nil(t, logger.Close()) +} + func BenchmarkRotateLogger(b *testing.B) { filename := "./test.log" filename2 := "./test2.log"