logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1382274 - in /logging/log4php/trunk/src: changes/changes.xml main/php/appenders/LoggerAppenderDailyFile.php main/php/appenders/LoggerAppenderFile.php test/php/appenders/LoggerAppenderDailyFileTest.php
Date Sat, 08 Sep 2012 09:57:22 GMT
Author: ihabunek
Date: Sat Sep  8 09:57:21 2012
New Revision: 1382274

URL: http://svn.apache.org/viewvc?rev=1382274&view=rev
Log:
LOG4PHP-120: Fixed LoggerAppenderDailyFile to rollover on date change in long running scipts.
Improved tests.

Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/main/php/appenders/LoggerAppenderDailyFile.php
    logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php
    logging/log4php/trunk/src/test/php/appenders/LoggerAppenderDailyFileTest.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1382274&r1=1382273&r2=1382274&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sat Sep  8 09:57:21 2012
@@ -21,6 +21,7 @@
 	</properties>
 	<body>
 		<release version="2.3.0" date="SVN">
+			<action date="2012-09-08" type="update" issue="LOG4PHP-120" dev="Ivan Habunek" due-to="Michal
Vanek" due-to-email="michal dot vanek at gmail dot com">Fixed LoggerAppenderDailyFile to
rollover on date change in long running scipts.</action>
 			<action date="2012-05-29" type="update" dev="Ivan Habunek">Removed $_ENV and $_SERVER
access from LoggerMDC.</action>
 		    <action date="2012-03-19" type="update" dev="Ivan Habunek">Made LoggerLayoutTTCC
deprecated.</action>
 			<action date="2012-02-28" type="fix" issue="LOG4PHP-163" dev="Ivan Habunek">Fixed
formatting bug in LoggerLayoutPattern.</action>

Modified: logging/log4php/trunk/src/main/php/appenders/LoggerAppenderDailyFile.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderDailyFile.php?rev=1382274&r1=1382273&r2=1382274&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderDailyFile.php (original)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderDailyFile.php Sat Sep  8 09:57:21
2012
@@ -48,6 +48,13 @@ class LoggerAppenderDailyFile extends Lo
 	 * @var string
 	 */
 	protected $datePattern = "Ymd";
+	
+	/**
+	 * Current date which was used when opening a file.
+	 * Used to determine if a rollover is needed when the date changes.
+	 * @var string
+	 */
+	protected $currentDate;
 
 	/** Additional validation for the date pattern. */
 	public function activateOptions() {
@@ -59,13 +66,50 @@ class LoggerAppenderDailyFile extends Lo
 			return;
 		}
 	}
+
+	/**
+	 * Appends a logging event.
+	 * 
+	 * If the target file changes because of passage of time (e.g. at midnight) 
+	 * the current file is closed. A new file, with the new date, will be 
+	 * opened by the write() method. 
+	 */
+	public function append(LoggerLoggingEvent $event) {
+		$eventDate = $this->getDate($event->getTimestamp());
+		
+		// Initial setting of current date
+		if (!isset($this->currentDate)) {
+			$this->currentDate = $eventDate;
+		} 
+		
+		// Check if rollover is needed
+		else if ($this->currentDate !== $eventDate) {
+			$this->currentDate = $eventDate;
+			
+			// Close the file if it's open.
+			// Note: $this->close() is not called here because it would set
+			//       $this->closed to true and the appender would not recieve
+			//       any more logging requests
+			if (is_resource($this->fp)) {
+				$this->write($this->layout->getFooter());
+				fclose($this->fp);
+			}
+			$this->fp = null;
+		}
+	
+		parent::append($event);
+	}
+	
+	/** Renders the date using the configured <var>datePattern<var>. */
+	protected function getDate($timestamp = null) {
+		return date($this->datePattern, $timestamp);
+	}
 	
 	/**
 	 * Determines target file. Replaces %s in file path with a date. 
 	 */
 	protected function getTargetFile() {
-		$date = date($this->datePattern);
-		return str_replace('%s', $date, $this->file);
+		return str_replace('%s', $this->currentDate, $this->file);
 	}
 	
 	/**

Modified: logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php?rev=1382274&r1=1382273&r2=1382274&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php (original)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php Sat Sep  8 09:57:21
2012
@@ -164,8 +164,8 @@ class LoggerAppenderFile extends LoggerA
 		if (is_resource($this->fp)) {
 			$this->write($this->layout->getFooter());
 			fclose($this->fp);
-			$this->fp = null;
 		}
+		$this->fp = null;
 		$this->closed = true;
 	}
 

Modified: logging/log4php/trunk/src/test/php/appenders/LoggerAppenderDailyFileTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/appenders/LoggerAppenderDailyFileTest.php?rev=1382274&r1=1382273&r2=1382274&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/appenders/LoggerAppenderDailyFileTest.php (original)
+++ logging/log4php/trunk/src/test/php/appenders/LoggerAppenderDailyFileTest.php Sat Sep 
8 09:57:21 2012
@@ -44,6 +44,121 @@ class LoggerAppenderDailyFileTest extend
 		self::assertInstanceOf('LoggerLayoutSimple', $actual);
 	}
 	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage Required parameter 'file' not set.
+	 */
+	public function testRequiredParamWarning1() {
+		$appender = new LoggerAppenderDailyFile();
+		$appender->activateOptions();
+	}
+	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage Required parameter 'datePattern' not set.
+	 */
+	public function testRequiredParamWarning2() {
+		$appender = new LoggerAppenderDailyFile();
+		$appender->setFile('file.log');
+		$appender->setDatePattern('');
+		$appender->activateOptions();
+	}
+	
+	public function testGetDatePattern() {
+		$appender = new LoggerAppenderDailyFile();
+
+		// Default pattern
+		$actual = $appender->getDatePattern();
+		self::assertEquals('Ymd', $actual);
+		
+		// Custom pattern
+		$appender->setDatePattern('xyz');
+		$actual = $appender->getDatePattern();
+		self::assertEquals('xyz', $actual);
+	}
+	
+	/**
+	 * For greater code coverage!
+	 * Override the warning so remaining code is reached.
+	 */
+	public function testRequiredParamWarning3() {
+		$appender = new LoggerAppenderDailyFile();
+		$appender->setFile('file.log');
+		$appender->setDatePattern('');
+		@$appender->activateOptions();
+	}
+	
+	public function testLazyFileOpen() {
+		$event = LoggerTestHelper::getWarnEvent("my message");
+		$file = PHPUNIT_TEMP_DIR . '/lazy-file.%s.log';
+		$pattern = 'Y-m-d'; 
+		
+		$date = date($pattern, $event->getTimeStamp());
+		$path =  PHPUNIT_TEMP_DIR . "/lazy-file.$date.log";
+		
+		if (file_exists($path)) {
+			unlink($path);
+		}
+		
+		$appender = new LoggerAppenderDailyFile();
+		$appender->setFile($file);
+		$appender->setDatePattern('Y-m-d');
+		$appender->activateOptions();
+		
+		// File should not exist before first append
+		self::assertFileNotExists($path);
+		$appender->append($event);
+		self::assertFileExists($path);
+	}
+	
+	public function testRollover()
+	{
+		$message = uniqid();
+		$level = LoggerLevel::getLevelDebug();
+		
+		$file = PHPUNIT_TEMP_DIR . '/lazy-file.%s.log';
+		$pattern = 'Y-m-d';
+		
+		// Get some timestamps for events - different date for each
+		$ts1 = mktime(10, 0, 0, 7, 3, 1980);
+		$ts2 = mktime(10, 0, 0, 7, 4, 1980);
+		$ts3 = mktime(10, 0, 0, 7, 5, 1980);
+		
+		$e1 = new LoggerLoggingEvent(__CLASS__, 'test', $level, $message, $ts1);
+		$e2 = new LoggerLoggingEvent(__CLASS__, 'test', $level, $message, $ts2);
+		$e3 = new LoggerLoggingEvent(__CLASS__, 'test', $level, $message, $ts3);
+		
+		// Expected paths
+		$path1 = PHPUNIT_TEMP_DIR . '/lazy-file.1980-07-03.log';
+		$path2 = PHPUNIT_TEMP_DIR . '/lazy-file.1980-07-04.log';
+		$path3 = PHPUNIT_TEMP_DIR . '/lazy-file.1980-07-05.log';
+		
+		@unlink($path1);
+		@unlink($path2);
+		@unlink($path3);
+
+		$appender = new LoggerAppenderDailyFile();
+		$appender->setFile($file);
+		$appender->setDatePattern('Y-m-d');
+		$appender->activateOptions();
+		
+		$appender->append($e1);
+		$appender->append($e2);
+		$appender->append($e3);
+		
+		$actual1 = file_get_contents($path1);
+		$actual2 = file_get_contents($path2);
+		$actual3 = file_get_contents($path3);
+		
+		$expected1 = "DEBUG - $message" . PHP_EOL;
+		$expected2 = "DEBUG - $message" . PHP_EOL;
+		$expected3 = "DEBUG - $message" . PHP_EOL;
+
+		self::assertSame($expected1, $actual1);
+		self::assertSame($expected2, $actual2);
+		self::assertSame($expected3, $actual3);
+	}
+	
 	public function testSimpleLogging() {
 		$event = LoggerTestHelper::getWarnEvent("my message");
 



Mime
View raw message