Author: floriansemm
Date: Fri Apr  6 16:50:17 2012
New Revision: 1310475

URL: http://svn.apache.org/viewvc?rev=1310475&view=rev
Log:
LOG4PHP-164: Compress rotated files with Gzip/Zip

Modified:
    logging/log4php/trunk/src/main/php/appenders/LoggerAppenderRollingFile.php
    
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderRollingFileTest.php

Modified: 
logging/log4php/trunk/src/main/php/appenders/LoggerAppenderRollingFile.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderRollingFile.php?rev=1310475&r1=1310474&r2=1310475&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderRollingFile.php 
(original)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderRollingFile.php 
Fri Apr  6 16:50:17 2012
@@ -33,6 +33,7 @@
  *                       reach before being rolled over to backup files.
  *                       Suffixes like "KB", "MB" or "GB" are allowed, f. e. 
"10KB" is interpreted as 10240
  * - maximumFileSize   - Alias to maxFileSize (deprecated, use "maxFileSize" 
instead)
+ * - compress             - Compress the rollover file ("true" or "false")
  *
  * <p>Contributors: Sergio Strampelli.</p>
  *
@@ -86,6 +87,16 @@ class LoggerAppenderRollingFile extends 
        private $expandedFileName = null;
 
        /**
+        * <p>The <var>Compress</var> option determindes the compression with 
zlib. 
+        * If set to true, then the rollover file is compressed and saved with 
the 
+        * file-extension .gz. 
+        * </p>
+        * 
+        * @var boolean Compress the rollover file
+        */
+       protected $compress = false;
+       
+       /**
         * Returns the value of the MaxBackupIndex option.
         * @return integer 
         */
@@ -119,20 +130,19 @@ class LoggerAppenderRollingFile extends 
 
                        // 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));
+                       } else {
+                               // Backup the active file
+                               copy($fileName, "$fileName.1");                 
        
+                       }
                }
                
                // Truncate the active file
@@ -140,6 +150,25 @@ class LoggerAppenderRollingFile extends 
                rewind($this->fp);
        }
        
+       private function renameArchievedLogs($fileName) {
+               for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
+                       
+                       $file = $fileName . "." . $i;
+                       if (true === $this->compress) {
+                               $file = $fileName . "." . $i .'.gz';
+                       }
+                       
+                       if(is_readable($file)) {
+                               $target = $fileName . '.' . ($i + 1);
+                               if (true === $this->compress) {
+                                       $target = $fileName . '.' . ($i + 1) . 
'.gz';
+                               }                               
+                               
+                               rename($file, $target);
+                       }
+               }               
+       }
+       
        public function setFile($fileName) {
                $this->file = $fileName;
                // As LoggerAppenderFile does not create the directory, it has 
to exist.
@@ -214,6 +243,16 @@ class LoggerAppenderRollingFile extends 
                } 
        }
        
+       public function activateOptions() {
+               parent::activateOptions();
+               
+               if ($this->compress == true && !function_exists('gzcompress')) {
+                       $this->closed = true;
+                       
+                       $this->warn('zlib is required for file-compression');
+               }
+       }
+       
        /**
         * @return Returns the maximum number of backup files to keep around.
         */
@@ -228,4 +267,8 @@ class LoggerAppenderRollingFile extends 
        public function getMaxFileSize() {
                return $this->maxFileSize;
        }
+       
+       public function setCompress($compress) {
+               $this->setBoolean('compress', $compress);
+       }
 }

Modified: 
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderRollingFileTest.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/appenders/LoggerAppenderRollingFileTest.php?rev=1310475&r1=1310474&r2=1310475&view=diff
==============================================================================
--- 
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderRollingFileTest.php 
(original)
+++ 
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderRollingFileTest.php 
Fri Apr  6 16:50:17 2012
@@ -33,6 +33,8 @@ class LoggerAppenderRollingFileTest exte
         */
        private $dir;
 
+       const WARNING_MASSAGE = 'WARN - my messageXYZ';
+       
        protected function setUp() {
                $this->dir = dirname(__FILE__) . '/../../../../target/phpunit';
                @mkdir($this->dir);
@@ -85,16 +87,25 @@ class LoggerAppenderRollingFileTest exte
                $expectedFilePattern = '/' . 
implode(preg_quote(DIRECTORY_SEPARATOR, '/'), array('target', 'phpunit', 
'doesnotexist\.log')) . '$/';
                self::assertEquals(preg_match($expectedFilePattern, 
$expandedFileName), 1, $expandedFileName);
        }
-
-       public function testSimpleLogging() {
+       
+       /**
+        * @return LoggerAppenderRollingFile
+        */
+       private function createRolloverAppender() {
                $layout = new LoggerLayoutSimple();
-
+               
                $appender = new LoggerAppenderRollingFile("mylogger");
                $appender->setFile($this->dir.'/TEST-rolling.txt');
                $appender->setLayout($layout);
                $appender->setMaxFileSize('1KB');
                $appender->setMaxBackupIndex(2);
                $appender->activateOptions();
+               
+               return $appender;
+       }
+
+       public function testSimpleLogging() {
+               $appender = $this->createRolloverAppender();
 
                $event = new LoggerLoggingEvent('LoggerAppenderFileTest',
                                                                                
new Logger('mycategory'),
@@ -121,37 +132,19 @@ class LoggerAppenderRollingFileTest exte
                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);
-               }
+               
$this->assertFalse(file_exists($this->dir.'/TEST-rolling.txt.3'), 'should not 
roll over three times');
        }
        
        public function testLoggingViaLogger() {
                $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->activateOptions();
+               
+               $appender = $this->createRolloverAppender();
 
                $logger->addAppender($appender);
                
@@ -169,30 +162,69 @@ class LoggerAppenderRollingFileTest exte
                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';
+               $this->checkFileContent($file);
+
+               
$this->assertFalse(file_exists($this->dir.'/TEST-rolling.txt.3'), 'should not 
roll over three times');
+       }
+       
+       public function testRolloverWithCompression() {
+               $logger = Logger::getLogger('mycat');
+               $logger->setAdditivity(false);
+
+               $appender = $this->createRolloverAppender();
+               $appender->setCompress(true);
+               
+               $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 = "WARN - my message123".PHP_EOL;
-               foreach($data as $r) {
-                       self::assertEquals($e, $r);
-               }
+               $e = self::WARNING_MASSAGE.PHP_EOL;
+               self::assertEquals($e, $line);
 
-               if(file_exists($this->dir.'/TEST-rolling.txt.3')) {
-                       self::assertTrue(false);
-               }
+               $firstCompressedRollingFile = 
$this->dir.'/TEST-rolling.txt.1.gz';
+               
$this->assertTrue(file_exists($firstCompressedRollingFile),'TEST-rolling.txt.1.gz
 not found');
+
+               $firstUncompressedRollingField = 
$this->dir.'/TEST-rolling.txt.1';
+               
$this->assertFalse(file_exists($firstUncompressedRollingField),'TEST-rolling.txt.1
 should be replaced by compressed');
+               
+               $secondCompressedRollingFile = 
$this->dir.'/TEST-rolling.txt.2.gz';
+               $this->assertTrue(file_exists($secondCompressedRollingFile), 
'TEST-rolling.txt.2.gz not found');
+               
+               $secondUncompressedRollingField = 
$this->dir.'/TEST-rolling.txt.2';
+               
$this->assertFalse(file_exists($secondUncompressedRollingField),'TEST-rolling.txt.2
 should be replaced by compressed');
+               
+       }       
+
+       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);
        }
 


Reply via email to