Skip to content

Commit

Permalink
Merge pull request #4 from iproj/upgrade
Browse files Browse the repository at this point in the history
RotationTime determined according to the pattern
  • Loading branch information
needkane authored Aug 24, 2021
2 parents e8c30a0 + 8956cc8 commit 867baa3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
51 changes: 48 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strconv"
"testing"
"time"

rotatelogs "github.com/iproj/file-rotatelogs"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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))
}
7 changes: 3 additions & 4 deletions internal/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion rotatelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions rotatelogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}),
},
}
Expand Down

0 comments on commit 867baa3

Please sign in to comment.