diff --git a/lumberjack.go b/lumberjack.go index 42b21a6..e137b0d 100644 --- a/lumberjack.go +++ b/lumberjack.go @@ -281,6 +281,8 @@ func backupName(name string, local bool) string { // would not put it over MaxSize. If there is no such file or the write would // put it over the MaxSize, a new file is created. func (l *Logger) openExistingOrNew(writeLen int) error { + l.mill() + filename := l.filename() info, err := os_Stat(filename) if os.IsNotExist(err) { diff --git a/lumberjack_test.go b/lumberjack_test.go index 5f33613..6a34add 100644 --- a/lumberjack_test.go +++ b/lumberjack_test.go @@ -369,7 +369,11 @@ func TestCleanupExistingBackups(t *testing.T) { newFakeTime() - b2 := []byte("foooooo!") + // Don't write enough to trigger a rotate or there is + // a race between whether or not there is one notification + // or two depending on how far through the millRunOnce method + // gets before the Write method calls rotate. + b2 := []byte("foo") n, err := l.Write(b2) isNil(err, t) equals(len(b2), n, t) @@ -634,6 +638,55 @@ func TestCompressOnRotate(t *testing.T) { fileCount(dir, 2, t) } +func TestCompressOnResume(t *testing.T) { + currentTime = fakeTime + megabyte = 1 + + dir := makeTempDir("TestCompressOnResume", t) + defer os.RemoveAll(dir) + + notify := make(chan struct{}) + filename := logFile(dir) + l := &Logger{ + Compress: true, + Filename: filename, + MaxSize: 10, + notifyCompressed: notify, + } + defer l.Close() + + // Create a backup file and empty "compressed" file. + filename2 := backupFile(dir) + b := []byte("foo!") + err := ioutil.WriteFile(filename2, b, 0644) + isNil(err, t) + err = ioutil.WriteFile(filename2+compressSuffix, []byte{}, 0644) + isNil(err, t) + + newFakeTime() + + b2 := []byte("boo!") + n, err := l.Write(b2) + isNil(err, t) + equals(len(b2), n, t) + existsWithContent(filename, b2, t) + + waitForNotify(notify, t) + + // The write should have started the compression - a compressed version of + // the log file should now exist and the original should have been removed. + bc := new(bytes.Buffer) + gz := gzip.NewWriter(bc) + _, err = gz.Write(b) + isNil(err, t) + err = gz.Close() + isNil(err, t) + existsWithContent(filename2+compressSuffix, bc.Bytes(), t) + notExist(filename2, t) + + fileCount(dir, 2, t) +} + func TestJson(t *testing.T) { data := []byte(` {