Index: src/main/php/appenders/LoggerAppenderRollingFile.php =================================================================== --- src/main/php/appenders/LoggerAppenderRollingFile.php (revision 1215479) +++ src/main/php/appenders/LoggerAppenderRollingFile.php (working copy) @@ -86,6 +86,13 @@ private $expandedFileName = null; /** + * + * compress the rollover file + * @var boolean + */ + protected $compress = false; + + /** * Returns the value of the MaxBackupIndex option. * @return integer */ @@ -119,20 +126,19 @@ // Delete the oldest file, to keep Windows happy. $file = $fileName . '.' . $this->maxBackupIndex; - if(is_writable($file)) + if(is_writable($file)) { unlink($file); + } // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2} - for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) { - $file = $fileName . "." . $i; - if(is_readable($file)) { - $target = $fileName . '.' . ($i + 1); - rename($file, $target); - } - } + $this->renameArchievedLogs($fileName); // Backup the active file copy($fileName, "$fileName.1"); + + if (true === $this->compress) { + file_put_contents('compress.zlib:///'.$fileName.'.1.gz', file_get_contents($fileName)); + } } // Truncate the active file @@ -140,6 +146,20 @@ rewind($this->fp); } + private function renameArchievedLogs($fileName) { + for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) { + $file = $fileName . "." . $i; + if(is_readable($file)) { + $target = $fileName . '.' . ($i + 1); + rename($file, $target); + + if (true === $this->compress) { + rename($file.'.gz', $target.'.gz'); + } + } + } + } + public function setFile($fileName) { $this->file = $fileName; // As LoggerAppenderFile does not create the directory, it has to exist. @@ -228,4 +248,8 @@ public function getMaxFileSize() { return $this->maxFileSize; } + + public function setCompress($compress) { + $this->setBoolean('compress', $compress); + } } Index: src/test/php/appenders/LoggerAppenderRollingFileTest.php =================================================================== --- src/test/php/appenders/LoggerAppenderRollingFileTest.php (revision 1215479) +++ src/test/php/appenders/LoggerAppenderRollingFileTest.php (working copy) @@ -33,6 +33,8 @@ */ private $dir; + const WARNING_MASSAGE = 'WARN - my messageXYZ'; + protected function setUp() { $this->dir = dirname(__FILE__) . '/../../../../target/phpunit'; @mkdir($this->dir); @@ -121,20 +123,10 @@ self::assertEquals($e, $line); $file = $this->dir.'/TEST-rolling.txt.1'; - $data = file($file); - $line = $data[count($data)-1]; - $e = "WARN - my message123".PHP_EOL; - foreach($data as $r) { - self::assertEquals($e, $r); - } + $this->checkFileContent($file); $file = $this->dir.'/TEST-rolling.txt.2'; - $data = file($file); - $line = $data[count($data)-1]; - $e = "WARN - my message123".PHP_EOL; - foreach($data as $r) { - self::assertEquals($e, $r); - } + $this->checkFileContent($file); if(file_exists($this->dir.'/TEST-rolling.txt.3')) { self::assertTrue(false); @@ -169,30 +161,70 @@ self::assertEquals($e, $line); $file = $this->dir.'/TEST-rolling.txt.1'; - $data = file($file); - $line = $data[count($data)-1]; - $e = "WARN - my message123".PHP_EOL; - foreach($data as $r) { - self::assertEquals($e, $r); - } + $this->checkFileContent($file); $file = $this->dir.'/TEST-rolling.txt.2'; - $data = file($file); - $line = $data[count($data)-1]; - $e = "WARN - my message123".PHP_EOL; - foreach($data as $r) { - self::assertEquals($e, $r); - } + $this->checkFileContent($file); if(file_exists($this->dir.'/TEST-rolling.txt.3')) { self::assertTrue(false); } } + + public function testRolloverWithCompression() { + $logger = Logger::getLogger('mycat'); + $logger->setAdditivity(false); + $layout = new LoggerLayoutSimple(); + $appender = new LoggerAppenderRollingFile("mylogger"); + $appender->setFile($this->dir.'/TEST-rolling.txt'); + $appender->setLayout($layout); + $appender->setMaxFileSize('1KB'); + $appender->setMaxBackupIndex(2); + $appender->setCompress(true); + $appender->activateOptions(); + + $logger->addAppender($appender); + + for($i = 0; $i < 1000; $i++) { + $logger->warn(self::WARNING_MASSAGE. $i); + } + + $logger->warn("my messageXYZ"); + + $file = $this->dir.'/TEST-rolling.txt'; + $data = file($file); + + $line = $data[count($data)-1]; + $e = self::WARNING_MASSAGE.PHP_EOL; + self::assertEquals($e, $line); + + $firstCompressedRollingFile = $this->dir.'/TEST-rolling.txt.1.gz'; + $this->assertTrue(file_exists($firstCompressedRollingFile),'TEST-rolling.txt.1.gz not found'); + + $secondCompressedRollingFile = $this->dir.'/TEST-rolling.txt.2.gz'; + $this->assertTrue(file_exists($secondCompressedRollingFile), 'TEST-rolling.txt.2.gz not found'); + } + + private function checkFileContent($file) { + $data = file($file); + $this->checkText($data); + } + + private function checkText($text) { + $line = $text[count($text)-1]; + $e = "WARN - my message123".PHP_EOL; + foreach($text as $r) { + self::assertEquals($e, $r); + } + } + protected function tearDown() { @unlink($this->dir.'/TEST-rolling.txt'); @unlink($this->dir.'/TEST-rolling.txt.1'); @unlink($this->dir.'/TEST-rolling.txt.2'); + @unlink($this->dir.'/TEST-rolling.txt.1.gz'); + @unlink($this->dir.'/TEST-rolling.txt.2.gz'); @rmdir($this->dir); }