diff --git a/rolling_file_appender.go b/rolling_file_appender.go index 1209f51..c719347 100644 --- a/rolling_file_appender.go +++ b/rolling_file_appender.go @@ -114,33 +114,21 @@ func (appender *RollingFileAppender) Close() error { } func (appender *RollingFileAppender) flush() error { - offset := 0 + n, err := appender.currentFile.Write(appender.buffer) - for offset < len(appender.buffer) { - if appender.currentFileSize >= appender.fileSize { - if err := appender.nextFile(); err != nil { - return err - } - } - - bytes := len(appender.buffer) - offset - maxBytes := appender.fileSize - appender.currentFileSize - - if bytes > maxBytes { - bytes = maxBytes - } + if err != nil { + return err + } - n, err := appender.currentFile.Write(appender.buffer[offset : offset+bytes]) + appender.buffer = appender.buffer[:0] + appender.currentFileSize += n - if err != nil { + if appender.currentFileSize >= appender.fileSize { + if err := appender.nextFile(); err != nil { return err } - - appender.currentFileSize += n - offset += n } - appender.buffer = appender.buffer[:0] return nil } diff --git a/rolling_file_appender_test.go b/rolling_file_appender_test.go index acc52a9..8c07dfa 100644 --- a/rolling_file_appender_test.go +++ b/rolling_file_appender_test.go @@ -58,7 +58,7 @@ func TestRollingFileAppender_Write(t *testing.T) { t.Fatalf("Appender must be created, but was: %v", err) } - for i := 0; i < 4; i++ { + for i := 0; i < 5; i++ { if n, err := rfa.Write([]byte(fmt.Sprintf("%d234\n", i))); err != nil || n != 5 { t.Fatalf("Log output must have been written, but was: %v %v", err, n) }