Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove max file size check in openExistingOrNew #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -269,10 +269,9 @@ 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.
func (l *Logger) openExistingOrNew(writeLen int) error {
// 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()

filename := l.filename()
Expand All @@ -284,10 +283,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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets say we set a size and the new check starts just at the border when the file is at max size but not yet at the tipping point, removing this check can potentially make the file not rotate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function in called within the write function where there is another check to ensure that the logs don't overflow.
if l.size+writeLen > l.max(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
Expand Down
38 changes: 38 additions & 0 deletions logrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading