Skip to content

Commit 0dc8bb3

Browse files
howbazaarSimonRichardson
authored andcommitted
Bring back the compress on resume functionality.
1 parent 1524e41 commit 0dc8bb3

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lumberjack.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ func backupName(name string, local bool) string {
281281
// would not put it over MaxSize. If there is no such file or the write would
282282
// put it over the MaxSize, a new file is created.
283283
func (l *Logger) openExistingOrNew(writeLen int) error {
284+
l.mill()
285+
284286
filename := l.filename()
285287
info, err := osStat(filename)
286288
if os.IsNotExist(err) {

lumberjack_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,11 @@ func TestCleanupExistingBackups(t *testing.T) {
366366

367367
newFakeTime()
368368

369-
b2 := []byte("foooooo!")
369+
// Don't write enough to trigger a rotate or there is
370+
// a race between whether or not there is one notification
371+
// or two depending on how far through the millRunOnce method
372+
// gets before the Write method calls rotate.
373+
b2 := []byte("foo")
370374
n, err := l.Write(b2)
371375
isNil(err, t)
372376
equals(len(b2), n, t)
@@ -631,6 +635,55 @@ func TestCompressOnRotate(t *testing.T) {
631635
fileCount(dir, 2, t)
632636
}
633637

638+
func TestCompressOnResume(t *testing.T) {
639+
currentTime = fakeTime
640+
megabyte = 1
641+
642+
dir := makeTempDir("TestCompressOnResume", t)
643+
defer os.RemoveAll(dir)
644+
645+
notify := make(chan struct{})
646+
filename := logFile(dir)
647+
l := &Logger{
648+
Compress: true,
649+
Filename: filename,
650+
MaxSize: 10,
651+
notifyCompressed: notify,
652+
}
653+
defer l.Close()
654+
655+
// Create a backup file and empty "compressed" file.
656+
filename2 := backupFile(dir)
657+
b := []byte("foo!")
658+
err := ioutil.WriteFile(filename2, b, 0644)
659+
isNil(err, t)
660+
err = ioutil.WriteFile(filename2+compressSuffix, []byte{}, 0644)
661+
isNil(err, t)
662+
663+
newFakeTime()
664+
665+
b2 := []byte("boo!")
666+
n, err := l.Write(b2)
667+
isNil(err, t)
668+
equals(len(b2), n, t)
669+
existsWithContent(filename, b2, t)
670+
671+
waitForNotify(notify, t)
672+
673+
// The write should have started the compression - a compressed version of
674+
// the log file should now exist and the original should have been removed.
675+
bc := new(bytes.Buffer)
676+
gz := gzip.NewWriter(bc)
677+
_, err = gz.Write(b)
678+
isNil(err, t)
679+
err = gz.Close()
680+
isNil(err, t)
681+
existsWithContent(filename2+compressSuffix, bc.Bytes(), t)
682+
notExist(filename2, t)
683+
684+
fileCount(dir, 2, t)
685+
}
686+
634687
func TestJson(t *testing.T) {
635688
data := []byte(`
636689
{

0 commit comments

Comments
 (0)