Skip to content

Commit

Permalink
Fixed splitting log lines in rolling file appender.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Feb 14, 2020
1 parent fb2f699 commit 61de9b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
28 changes: 8 additions & 20 deletions rolling_file_appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion rolling_file_appender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 61de9b6

Please sign in to comment.