Skip to content

Commit

Permalink
Bring back the compress on resume functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
howbazaar committed Mar 27, 2020
1 parent 6dba581 commit c81c014
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
55 changes: 54 additions & 1 deletion lumberjack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(`
{
Expand Down

0 comments on commit c81c014

Please sign in to comment.