From 8956cc8fa5b6a856a5e155990cd59cc29368a622 Mon Sep 17 00:00:00 2001 From: needkane Date: Tue, 24 Aug 2021 23:46:26 +0800 Subject: [PATCH] RotationTime determined according to the pattern --- example_test.go | 51 ++++++++++++++++++++++++++++++++--- internal/fileutil/fileutil.go | 7 +++-- options.go | 1 + rotatelogs.go | 6 ++++- rotatelogs_test.go | 8 +++--- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/example_test.go b/example_test.go index 5412c76..df8023b 100644 --- a/example_test.go +++ b/example_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strconv" "testing" + "time" rotatelogs "github.com/iproj/file-rotatelogs" "github.com/stretchr/testify/assert" @@ -73,7 +74,7 @@ func TestTooMuchLog(t *testing.T) { testDir = "test_much_log" testLogPath = filepath.Join(testDir, "access_log") rotationCount = 3 - N = 12 // N < log.Printf content size + N = 12 // N > 10 ) err := os.Mkdir(testDir, 0777) assert.Nil(t, err) @@ -84,7 +85,7 @@ func TestTooMuchLog(t *testing.T) { testLogPath+".%Y%m%d%H%M", rotatelogs.WithLinkName(testLogPath), rotatelogs.WithRotationCount(uint(rotationCount)), - rotatelogs.WithRotationSize(12), + rotatelogs.WithRotationSize(12), // Log contentSize > 12 ) assert.Nil(t, err) @@ -93,10 +94,54 @@ func TestTooMuchLog(t *testing.T) { log.Printf("Test content %d\n", i) } files, _ := ioutil.ReadDir(testDir) - fmt.Println(files) assert.Equal(t, rotationCount+1, len(files)) bytez, err := ioutil.ReadFile(testLogPath) assert.Nil(t, err) assert.Equal(t, strconv.Itoa(N-1), string(bytez[len(bytez)-3:len(bytez)-1])) } + +func TestSizePriorityOverTime(t *testing.T) { + + var ( + testDir = "test_size_priority_over_time" + testLogPath = filepath.Join(testDir, "access_log") + rotationCount = 2 + N = 12 + ) + err := os.Mkdir(testDir, 0777) + assert.Nil(t, err) + defer os.RemoveAll(testDir) + assert.Nil(t, err) + + rl, err := rotatelogs.New( + testLogPath+".%Y%m%d%H%M%S", // Accurate to seconds + rotatelogs.WithRotationCount(uint(rotationCount)), + rotatelogs.WithRotationSize(12000), + ) + assert.Nil(t, err) + + log.SetOutput(rl) + for i := 0; i < N; i++ { + log.Printf("Test content %d\n", i) + // N * sleepTime > 1s + time.Sleep(120 * time.Millisecond) + } + files, _ := ioutil.ReadDir(testDir) + assert.Equal(t, 1, len(files)) // N * contentSize < rotationSize + + rl, err = rotatelogs.New( + testLogPath+".%Y%m%d%H%M%S", // Accurate to seconds + rotatelogs.WithRotationCount(uint(rotationCount)), + ) + assert.Nil(t, err) + + log.SetOutput(rl) + for i := 0; i < N; i++ { + log.Printf("Test content %d\n", i) + // N * sleepTime > 1s + time.Sleep(120 * time.Millisecond) + } + files, _ = ioutil.ReadDir(testDir) + assert.Equal(t, rotationCount, len(files)) +} diff --git a/internal/fileutil/fileutil.go b/internal/fileutil/fileutil.go index 346f4fb..e9b5667 100644 --- a/internal/fileutil/fileutil.go +++ b/internal/fileutil/fileutil.go @@ -14,7 +14,9 @@ import ( // // The bsase time that is used to generate the filename is truncated based // on the rotation time. -func GenerateFn(pattern *strftime.Strftime, clock interface{ Now() time.Time }, rotationTime time.Duration) string { +func GenerateFn(pattern *strftime.Strftime, clock interface { + Now() time.Time +}) string { now := clock.Now() // XXX HACK: Truncate only happens in UTC semantics, apparently. @@ -30,10 +32,7 @@ func GenerateFn(pattern *strftime.Strftime, clock interface{ Now() time.Time }, var base time.Time if now.Location() != time.UTC { base = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), time.UTC) - base = base.Truncate(rotationTime) base = time.Date(base.Year(), base.Month(), base.Day(), base.Hour(), base.Minute(), base.Second(), base.Nanosecond(), base.Location()) - } else { - base = now.Truncate(rotationTime) } return pattern.FormatString(base) diff --git a/options.go b/options.go index e9e82d9..d8580bf 100644 --- a/options.go +++ b/options.go @@ -55,6 +55,7 @@ func WithMaxAge(d time.Duration) Option { return option.New(optkeyMaxAge, d) } +// Deprecated, rotationTime use pattern // WithRotationTime creates a new Option that sets the // time between rotation. func WithRotationTime(d time.Duration) Option { diff --git a/rotatelogs.go b/rotatelogs.go index 05df293..7abbc97 100644 --- a/rotatelogs.go +++ b/rotatelogs.go @@ -122,7 +122,7 @@ func (rl *RotateLogs) getWriterNolock(bailOnRotateFail, useGenerationalNames boo // This filename contains the name of the "NEW" filename // to log to, which may be newer than rl.currentFilename - baseFn := fileutil.GenerateFn(rl.pattern, rl.clock, rl.rotationTime) + baseFn := fileutil.GenerateFn(rl.pattern, rl.clock) filename := baseFn var forceNewFile bool @@ -134,6 +134,10 @@ func (rl *RotateLogs) getWriterNolock(bailOnRotateFail, useGenerationalNames boo } if baseFn != rl.curBaseFn { + if rl.rotationSize > 0 && fi != nil && !sizeRotation { + // Nothing to do + return rl.outFh, nil + } generation = 0 // even though this is the first write after calling New(), // check if a new file needs to be created diff --git a/rotatelogs_test.go b/rotatelogs_test.go index d812b19..f907960 100644 --- a/rotatelogs_test.go +++ b/rotatelogs_test.go @@ -11,8 +11,8 @@ import ( "testing" "time" - "github.com/jonboulle/clockwork" rotatelogs "github.com/iproj/file-rotatelogs" + "github.com/jonboulle/clockwork" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -450,15 +450,15 @@ func TestGHIssue23(t *testing.T) { Clock rotatelogs.Clock }{ { - Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1))+".201806010000.log"), + Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1))+".201806010318.log"), Clock: ClockFunc(func() time.Time { return time.Date(2018, 6, 1, 3, 18, 0, 0, loc) }), }, { - Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1))+".201712310000.log"), + Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1))+".201712312352.log"), Clock: ClockFunc(func() time.Time { - return time.Date(2017, 12, 31, 23, 52, 0, 0, loc) + return time.Date(2017, 12, 31, 23, 52, 1, 12, loc) }), }, }