Skip to content

Commit

Permalink
fix: rotating every line after first interval
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Mar 18, 2024
1 parent 15832ba commit 5621e17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions rotate_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (r *RotateFile) rotate() {
r.file.Close()
}
r.file = file
r.written = 0
r.lastRotate = now
}
}
Expand Down
35 changes: 19 additions & 16 deletions rotate_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,44 @@ func TestRotateSize(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(path)

file, err := NewRotateFile(path, "test", 10)
file, err := NewRotateFile(path, "test", 100)
require.NoError(t, err)

_, err = file.Write([]byte("12345678901234567890"))
require.NoError(t, err)
_, err = file.Write([]byte("12345678901234567890"))
require.NoError(t, err)
b := []byte("test\n")
for range 100 {
require.NoError(t, err)
_, err = file.Write(b)
require.NoError(t, err)
}

dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 3)
require.Len(t, dir, 5)
}

func TestRotateInterval(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestRotateInterval")
require.NoError(t, err)
defer os.RemoveAll(path)

file, err := NewRotateFile(path, "test", 1024, WithCheckEveryN(2), WithRotateInterval(1*time.Second))
file, err := NewRotateFile(path, "test", 1024, WithCheckEveryN(1), WithRotateInterval(100*time.Millisecond))
require.NoError(t, err)

_, err = file.Write([]byte("1"))
require.NoError(t, err)
_, err = file.Write([]byte("2"))
require.NoError(t, err)
b := []byte("test\n")
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}

dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 1)

time.Sleep(1 * time.Second)
_, err = file.Write([]byte("1"))
require.NoError(t, err)
_, err = file.Write([]byte("2"))
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
for range 5 {
_, err = file.Write(b)
require.NoError(t, err)
}

dir, err = os.ReadDir(path)
require.NoError(t, err)
Expand Down

0 comments on commit 5621e17

Please sign in to comment.