logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1164925 - in /logging/log4php/trunk/src: changes/ examples/php/ examples/resources/ main/php/appenders/ site/apt/docs/appender/ test/php/appenders/
Date Sat, 03 Sep 2011 21:07:31 GMT
Author: ihabunek
Date: Sat Sep  3 21:07:31 2011
New Revision: 1164925

URL: http://svn.apache.org/viewvc?rev=1164925&view=rev
Log:
LOG4PHP-145: A rewrite of the syslog appender so it parses options correctly. Also updated
docs and example.

Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/examples/php/appender_syslog.php
    logging/log4php/trunk/src/examples/resources/appender_syslog.properties
    logging/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php
    logging/log4php/trunk/src/site/apt/docs/appender/appender.apt
    logging/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sat Sep  3 21:07:31 2011
@@ -21,6 +21,7 @@
 	</properties>
 	<body>
 	    <release version="2.2.0" date="SVN">
+	    	<action date="2011-09-03" type="fix" issue="LOG4PHP-145" dev="Ivan Habunek">The
syslog appender does not correctly parse options</action>
 	    	<action date="2011-08-31" type="remove" issue="LOG4PHP-149" dev="Ivan Habunek">Remove
deprecated appender LoggerAppenderAdodb</action>
 	    	<action date="2011-08-31" type="fix" issue="LOG4PHP-148" dev="Ivan Habunek">LoggerUserFieldPatternConverter
class missing in log4php archive</action>
 	        <action date="2011-07-14" type="fix" issue="LOG4PHP-143" dev="Ivan Habunek" due-to="Justin
Cherniak" due-to-email="justin dot cherniak at gmail dot com">LoggerConfiguratorPhp does
not accept integer constants for appender threshold</action>

Modified: logging/log4php/trunk/src/examples/php/appender_syslog.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/examples/php/appender_syslog.php?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/examples/php/appender_syslog.php (original)
+++ logging/log4php/trunk/src/examples/php/appender_syslog.php Sat Sep  3 21:07:31 2011
@@ -20,4 +20,4 @@ require_once dirname(__FILE__).'/../../m
 
 Logger::configure(dirname(__FILE__).'/../resources/appender_syslog.properties');
 $logger = Logger::getRootLogger();
-$logger->fatal("Hello World!");
+$logger->info("Hello World!");

Modified: logging/log4php/trunk/src/examples/resources/appender_syslog.properties
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/examples/resources/appender_syslog.properties?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/examples/resources/appender_syslog.properties (original)
+++ logging/log4php/trunk/src/examples/resources/appender_syslog.properties Sat Sep  3 21:07:31
2011
@@ -18,5 +18,6 @@
 log4php.appender.default = LoggerAppenderSyslog
 log4php.appender.default.layout = LoggerLayoutSimple
 log4php.appender.default.ident = log4php-test
-log4php.appender.default.facility = LOG_LOCAL0
+log4php.appender.default.facility = USER
+log4php.appender.default.option = "PID|CONS|NDELAY"
 log4php.rootLogger = DEBUG, default

Modified: logging/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php (original)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php Sat Sep  3 21:07:31
2011
@@ -19,28 +19,47 @@
  */
 
 /**
- * Log events using php {@link PHP_MANUAL#syslog} function.
+ * Log events to a system log using the {@link PHP_MANUAL#syslog} function.
  *
- * This appender can be configured by changing the following attributes:
+ * This appenders requires a layout.
+ *
+ * Configurable parameters:
  * 
- * - layout           - Sets the layout class for this appender
- * - ident            - Set the ident of the syslog message.
- * - priority         - Set the priority value for the syslog message.
- * - facility         - Set the facility value for the syslog message
- * - overridePriority - If the priority of the message to be sent can be 
- *                      defined by a value in the properties-file, set 
- *                      parameter value to "true"
- * - option           - Set the option value for the syslog message. 
- *                      This value is used as a parameter for php openlog()
- *                      and passed on to the syslog daemon.
+ * - ident            - The ident of the syslog message.
+ * - priority         - The priority for the syslog message (used when overriding priority).
+ * - facility         - The facility for the syslog message
+ * - overridePriority - If set to true, the message priority will always use 
+ *                      the value defined in {@link $priority}, otherwise the
+ *                      priority will be determined by the message's log level.  
+ * - option           - The option value for the syslog message. 
+ *
+ * Recognised syslog options are:
+ * 	- CONS 	 - if there is an error while sending data to the system logger, write directly
to the system console
+ * 	- NDELAY - open the connection to the logger immediately
+ * 	- ODELAY - delay opening the connection until the first message is logged (default)
+ * 	- PERROR - print log message also to standard error
+ * 	- PID    - include PID with each message
+ * 
+ * Multiple options can be set by delimiting them with a pipe character, 
+ * e.g.: "CONS|PID|PERROR".
+ * 
+ * Recognised syslog priorities are:
+ * 	- EMERG
+ * 	- ALERT
+ * 	- CRIT
+ * 	- ERR
+ * 	- WARNING
+ * 	- NOTICE
+ * 	- INFO
+ * 	- DEBUG
  *
  * Levels are mapped as follows:
- * - <b>level >= FATAL</b> to LOG_ALERT
- * - <b>FATAL > level >= ERROR</b> to LOG_ERR 
- * - <b>ERROR > level >= WARN</b> to LOG_WARNING
- * - <b>WARN  > level >= INFO</b> to LOG_INFO
- * - <b>INFO  > level >= DEBUG</b> to LOG_DEBUG
- * - <b>DEBUG > level >= TRACE</b> to LOG_DEBUG
+ * - <b>FATAL</b> to LOG_ALERT
+ * - <b>ERROR</b> to LOG_ERR 
+ * - <b>WARN</b> to LOG_WARNING
+ * - <b>INFO</b> to LOG_INFO
+ * - <b>DEBUG</b> to LOG_DEBUG
+ * - <b>TRACE</b> to LOG_DEBUG
  *
  * An example:
  *
@@ -56,114 +75,173 @@ class LoggerAppenderSyslog extends Logge
 	
 	/**
 	 * The ident string is added to each message. Typically the name of your application.
-	 *
-	 * @var string Ident for your application
+	 * 
+	 * @var string 
 	 */
-	private $_ident = "Log4PHP Syslog-Event";
+	private $ident = "Apache log4php";
 
 	/**
-	 * The priority parameter value indicates the level of importance of the message.
-	 * It is passed on to the Syslog daemon.
+	 * The syslog priority to use when overriding priority. This setting is 
+	 * required if {@link overridePriority} is set to true. 
 	 * 
-	 * @var int Syslog priority
+	 * @var string 
 	 */
-	private $_priority;
+	private $priority;
 	
 	/**
-	 * The option used when generating a log message.
-	 * It is passed on to the Syslog daemon.
+	 * The option used when opening the syslog connection.
 	 * 
-	 * @var int Syslog priority
+	 * @var string
 	 */
-	private $_option;
+	private $option = 'PID|CONS';
 	
 	/**
 	 * The facility value indicates the source of the message.
-	 * It is passed on to the Syslog daemon.
 	 *
-	 * @var const int Syslog facility
+	 * @var string
 	 */
-	private $_facility;
+	private $facility = 'USER';
 	
 	/**
-	 * If it is necessary to define logging priority in the .properties-file,
-	 * set this variable to "true".
+	 * If set to true, the message priority will always use the value defined 
+	 * in {@link $priority}, otherwise the priority will be determined by the 
+	 * message's log level.
 	 *
-	 * @var const int value indicating whether the priority of the message is defined in the
.properties-file
-	 *				   (or properties-array)
+	 * @var string
 	 */
-	private $_overridePriority;
-
-	/** @var indiciates if this appender should run in dry mode */
-	private $dry = false;
-
-	public function __construct($name = '') {
-		parent::__construct($name);
-	}
+	private $overridePriority = false;
 
+	/**
+	 * Holds the int value of the {@link $priority}.
+	 * @var int
+	 */
+	private $intPriority;
+	
+	/**
+	 * Holds the int value of the {@link $facility}.
+	 * @var int
+	 */
+	private $intFacility;
+	
+	/**
+	 * Holds the int value of the {@link $option}.
+	 * @var int
+	 */
+	private $intOption;
+	
+	/** Maps log4php levels to equivalent syslog priorities. */
+	private $levelMap = array(
+		LoggerLevel::TRACE => LOG_DEBUG,
+		LoggerLevel::DEBUG => LOG_DEBUG,
+		LoggerLevel::INFO  => LOG_INFO,
+		LoggerLevel::WARN  => LOG_WARNING,
+		LoggerLevel::ERROR => LOG_ERR,
+		LoggerLevel::FATAL => LOG_ALERT,
+	);
+	
 	public function __destruct() {
 		$this->close();
 	}
-	
-	public function setDry($dry) {
-		$this->dry = $dry;
-	}
-	
+
 	/**
-	 * Set the ident of the syslog message.
+	 * Sets the {@link $ident}.
 	 *
-	 * @param string Ident
+	 * @param string $ident
 	 */
 	public function setIdent($ident) {
-		$this->_ident = $ident; 
+		$this->ident = $ident; 
 	}
-
+	
 	/**
-	 * Set the priority value for the syslog message.
+	 * Sets the {@link $priority}.
 	 *
-	 * @param const int Priority
+	 * @param string $priority
 	 */
 	public function setPriority($priority) {
-		$this->_priority = $priority;
+		$this->priority = $priority;
 	}
 	
-	
 	/**
-	 * Set the facility value for the syslog message.
+	 * Sets the {@link $facility}.
 	 *
-	 * @param const int Facility
+	 * @param string $facility
 	 */
 	public function setFacility($facility) {
-		$this->_facility = $facility;
+		$this->facility = $facility;
 	} 
 	
 	/**
-	 * If the priority of the message to be sent can be defined by a value in the properties-file,

-	 * set parameter value to "true".
+	 * Sets the {@link $overridePriority}.
 	 *
-	 * @param bool Override priority
+	 * @param string $overridePriority
 	 */
 	public function setOverridePriority($overridePriority) {
-		$this->_overridePriority = $overridePriority;
+		$this->overridePriority = $overridePriority;
 	} 
 	
 	/**
-	 * Set the option value for the syslog message.
-	 * This value is used as a parameter for php openlog()	
-	 * and passed on to the syslog daemon.
+	* Sets the {@link $option}.
+	*
+	* @param string $option
+	*/
+	public function setOption($option) {
+		$this->option = $option;
+	}
+	
+	/**
+	* Returns the {@link $ident}.
+	*
+	* @return string $ident
+	*/
+	public function getIdent() {
+		return $this->ident;
+	}
+	
+	/**
+	 * Returns the {@link $priority}.
 	 *
-	 * @param string	$option
+	 * @return string
 	 */
-	public function setOption($option) {
-		$this->_option = $option;
+	public function getPriority() {
+		return $this->priority;
+	}
+	
+	/**
+	 * Returns the {@link $facility}.
+	 *
+	 * @return string
+	 */
+	public function getFacility() {
+		return $this->facility;
+	}
+	
+	/**
+	 * Returns the {@link $overridePriority}.
+	 *
+	 * @return string
+	 */
+	public function getOverridePriority() {
+		return $this->overridePriority;
 	}
 	
+	/**
+	 * Returns the {@link $option}.
+	 *
+	 * @return string
+	 */
+	public function getOption() {
+		return $this->option;
+	}
+	
+	
 	public function activateOptions() {
-		// Deprecated as of 5.3 and removed in 6.0
-		// define_syslog_variables();
+		$this->intPriority = $this->parsePriority();
+		$this->intOption   = $this->parseOption();
+		$this->intFacility = $this->parseFacility();
+		
 		$this->closed = false;
 	}
-
+	
 	public function close() {
 		if($this->closed != true) {
 			closelog();
@@ -171,44 +249,78 @@ class LoggerAppenderSyslog extends Logge
 		}
 	}
 
+	/** 
+	 * Appends the event to syslog.
+	 * 
+	 * Log is opened and closed each time because if it is not closed, it
+	 * can cause the Apache httpd server to log to whatever ident/facility 
+	 * was used in openlog().
+	 *
+	 * @see http://www.php.net/manual/en/function.syslog.php#97843
+	 */
 	public function append(LoggerLoggingEvent $event) {
-		if($this->_option == NULL){
-			$this->_option = LOG_PID | LOG_CONS;
+		$priority = $this->getSyslogPriority($event->getLevel());
+		$message = $this->layout->format($event);
+	
+		openlog($this->ident, $this->intOption, $this->intFacility);
+		syslog($priority, $message);
+		closelog();
+	}
+	
+	/** Determines which syslog priority to use based on the given level. */
+	private function getSyslogPriority(LoggerLevel $level) {
+		if($this->overridePriority) {
+			return $this->intPriority;
 		}
 		
-		$level	 = $event->getLevel();
-		if($this->layout === null) {
-			$message = $event->getRenderedMessage();
+		$int = $level->toInt();
+		
+		if (isset($this->levelMap[$int])) {
+			return $this->levelMap[$int];
 		} else {
-			$message = $this->layout->format($event); 
+			return LOG_DEBUG;
 		}
-
-		// If the priority of a syslog message can be overridden by a value defined in the properties-file,
-		// use that value, else use the one that is defined in the code.
-		if(!$this->dry) {
-			// Attach the process ID to the message, use the facility defined in the .properties-file
-			openlog($this->_ident, $this->_option, $this->_facility);
-		
-			if($this->_overridePriority) {
-				syslog($this->_priority, $message);			   
-			} else {
-				if($level->isGreaterOrEqual(LoggerLevel::getLevelFatal())) {
-					syslog(LOG_ALERT, $message);
-				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelError())) {
-					syslog(LOG_ERR, $message);		  
-				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelWarn())) {
-					syslog(LOG_WARNING, $message);
-				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelInfo())) {
-					syslog(LOG_INFO, $message);
-				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelDebug())) {
-					syslog(LOG_DEBUG, $message);
-				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelTrace())) {
-					syslog(LOG_DEBUG, $message);	// No trace level in syslog
+	}
+	
+	/** Parses a syslog option string and returns the correspodning int value. */
+	private function parseOption() {
+		$value = 0;
+		$options = explode('|', $this->option);
+	
+		foreach($options as $option) {
+			if (!empty($option)) {
+				$constant = "LOG_" . trim($option);
+				if (defined($constant)) {
+					$value |= constant($constant);
+				} else {
+					trigger_error("log4php: Invalid syslog option provided: $option. Whole option string:
{$this->option}.", E_USER_WARNING);
 				}
 			}
-			closelog();
-		} else {
-			echo "DRY MODE OF SYSLOG APPENDER: ".$message;
 		}
+		return $value;
+	}
+	
+	/** Parses the facility string and returns the corresponding int value. */
+	private function parseFacility() {
+		if (!empty($this->facility)) {   
+			$constant = "LOG_" . trim($this->facility);
+			if (defined($constant)) {
+				return constant($constant);
+			} else {
+				trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);
+			}
+		}
+	}
+
+	/** Parses the priority string and returns the corresponding int value. */
+	private function parsePriority() {
+		if (!empty($this->priority)) {
+			$constant = "LOG_" . trim($this->priority);
+			if (defined($constant)) {
+				return constant($constant);
+			} else {
+				trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);
+			}
+		}	
 	}
 }

Modified: logging/log4php/trunk/src/site/apt/docs/appender/appender.apt
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/site/apt/docs/appender/appender.apt?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/site/apt/docs/appender/appender.apt (original)
+++ logging/log4php/trunk/src/site/apt/docs/appender/appender.apt Sat Sep  3 21:07:31 2011
@@ -728,7 +728,7 @@ log4php.appender.default.locationInfo = 
 *-------------------+--------------*----------------------+------------------------+
 || Parameter        || Required    || Default value       || Description
 *-------------------+--------------*----------------------+------------------------+
-| ident             | No           | Log4PHP Syslog-Event | A string which will identify
your appender.  
+| ident             | No           | Apache log4php       | A string which will identify
your appender.  
 *-------------------+--------------*----------------------+------------------------+
 | overridePriority  | No           | false                | If set to true, all messages
will be sent to the syslog using the priority specified in the <priority> parameter.
Otherwise, the pririty will depend on the level of the event being logged. See below. 
 *-------------------+--------------*----------------------+------------------------+
@@ -842,7 +842,11 @@ log4php.appender.default.locationInfo = 
 | PID     | Include the PID with each message.
 *---------+--------------------------------------------+
 
-  Multiple options may be set by separating them with a "|". For exampe "CONS|PID|NODELAY".
+  Multiple options may be set by separating them with a pipe character. For exampe "CONS|PID|NODELAY".
+  
++--
+NOTE: when setting multiple options in an INI file, be sure to put the options string in
quotes. Otherwise they will not be parsed correctly.
++--
 
 *** Examples 
 
@@ -864,5 +868,5 @@ log4php.appender.default = LoggerAppende
 log4php.appender.default.layout = LoggerLayoutSimple
 log4php.appender.default.ident = log4php-test
 log4php.appender.default.facility = LOCAL0
-log4php.appender.default.options = NDELAY|PID
+log4php.appender.default.options = "NDELAY|PID"
 +--

Modified: logging/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php?rev=1164925&r1=1164924&r2=1164925&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php (original)
+++ logging/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php Sat Sep  3 21:07:31
2011
@@ -6,16 +6,16 @@
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
- * @category   tests   
+ *
+ * @category   tests
  * @package    log4php
  * @subpackage appenders
  * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
@@ -24,31 +24,240 @@
  */
 
 /**
+ * Tests the syslog appender.
+ * 
+ * Many of these tests rely on reflection features introduced in 5.3 and 
+ * will be skipped if run on a lower version. 
+ * 
+ * This test will only write a single entry to the syslog.
+ * 
  * @group appenders
  */
 class LoggerAppenderSyslogTest extends PHPUnit_Framework_TestCase {
-        
+
+	public function testSettersGetters() {
+		
+		// Setters should accept any value, without validation 
+		$expected = "Random string value";
+		
+		$appender = new LoggerAppenderSyslog();
+		$appender->setIdent($expected);
+		$appender->setFacility($expected);
+		$appender->setOverridePriority($expected);
+		$appender->setPriority($expected);
+		$appender->setOption($expected);
+		
+		$actuals = array(
+			$appender->getIdent(),
+			$appender->getFacility(),
+			$appender->getOverridePriority(),
+			$appender->getPriority(),
+			$appender->getOption()
+		);
+		
+		foreach($actuals as $actual) {
+			$this->assertSame($expected, $actual);
+		}
+	}
+	
 	public function testRequiresLayout() {
 		$appender = new LoggerAppenderSyslog();
-		self::assertTrue($appender->requiresLayout());
+		$this->assertTrue($appender->requiresLayout());
 	}
 	
-	public function testSyslog() {
-		$appender = new LoggerAppenderSyslog("myname ");
-		
-		$layout = new LoggerLayoutSimple();
-		$appender->setLayout($layout);
-		$appender->setDry(true);
+	public function testLogging() {
+		$appender = new LoggerAppenderSyslog("myname");
+		$appender->setLayout(new LoggerLayoutSimple());
 		$appender->activateOptions();
-		$event = new LoggerLoggingEvent("LoggerAppenderSyslogTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		 
-		ob_start();
+		
+		$event = new LoggerLoggingEvent(__CLASS__, new Logger("TestLogger"), LoggerLevel::getLevelError(),
"testmessage");
 		$appender->append($event);
-		$v = ob_get_contents();
-		ob_end_clean();
+	}
+
+	/** Tests parsing of "option" parameter. */
+	public function testOption() {
 		
-		$e = "DRY MODE OF SYSLOG APPENDER: ERROR - testmessage".PHP_EOL;
-		self::assertEquals($v, $e);
-    }
-    
+		if(!class_exists("ReflectionClass") || method_exists('ReflectionProperty', 'setAccessible
')) {
+			$this->markTestSkipped("ReflectionClass required to perform this test.");
+		}
+		
+		$options = array(
+			'CONS' => LOG_CONS,
+			'NDELAY' => LOG_NDELAY,
+			'ODELAY' => LOG_ODELAY,
+			'PERROR' => LOG_PERROR,
+			'PID' => LOG_PID,
+			
+			// test some combinations
+			'CONS|NDELAY' => LOG_CONS | LOG_NDELAY,
+			'PID|PERROR' => LOG_PID | LOG_PERROR,
+			'CONS|PID|NDELAY' => LOG_CONS | LOG_PID | LOG_NDELAY
+		);
+
+		// Defaults
+		$defaultStr = "PID|CONS";
+		$default = LOG_PID | LOG_CONS;
+		
+		// This makes reading of a private property possible
+		$property = new ReflectionProperty('LoggerAppenderSyslog', 'intOption');
+		$property->setAccessible(true);
+		
+		// Check default value first
+		$appender = new LoggerAppenderSyslog();
+		$appender->activateOptions();
+		$actual = $property->getValue($appender);
+		$this->assertSame($default, $actual, "Failed setting default option [$defaultStr]");
+		
+		foreach($options as $option => $expected) {
+			$appender = new LoggerAppenderSyslog();
+			$appender->setOption($option);
+			$appender->activateOptions();
+			
+			$actual = $property->getValue($appender);
+			$this->assertSame($expected, $actual, "Failed setting option [$option].");
+		}
+	}
+	
+	/** Tests parsing of "priority" parameter. */
+	public function testPriority() {
+	
+		if(!class_exists("ReflectionClass") || method_exists('ReflectionProperty', 'setAccessible
')) {
+			$this->markTestSkipped("ReflectionClass required to perform this test.");
+		}
+	
+		$default = null;
+		$defaultStr = 'null';
+	
+		$priorities = array(
+			'EMERG' => LOG_EMERG,
+			'ALERT' => LOG_ALERT,
+			'CRIT' => LOG_CRIT,
+			'ERR' => LOG_ERR,
+			'WARNING' => LOG_WARNING,
+			'NOTICE' => LOG_NOTICE,
+			'INFO' => LOG_INFO,
+			'DEBUG' => LOG_DEBUG
+		);
+	
+		// This makes reading of a private property possible
+		$property = new ReflectionProperty('LoggerAppenderSyslog', 'intPriority');
+		$property->setAccessible(true);
+	
+		// Check default value first
+		$appender = new LoggerAppenderSyslog();
+		$appender->activateOptions();
+		$actual = $property->getValue($appender);
+		$this->assertSame($default, $actual, "Failed setting default priority [$defaultStr].");
+	
+		foreach($priorities as $priority => $expected) {
+			$appender = new LoggerAppenderSyslog();
+			$appender->setPriority($priority);
+			$appender->activateOptions();
+				
+			$actual = $property->getValue($appender);
+			$this->assertSame($expected, $actual, "Failed setting priority [$priority].");
+		}
+	}
+	
+	/** Tests parsing of "facility" parameter. */
+	public function testFacility() {
+	
+		if(!class_exists("ReflectionClass") || method_exists('ReflectionProperty', 'setAccessible
')) {
+			$this->markTestSkipped("ReflectionClass required to perform this test.");
+		}
+	
+		// Default value is the same on all OSs
+		$default = LOG_USER;
+		$defaultStr = 'USER';
+
+		// All possible facility strings (some of which might not exist depending on the OS)
+		$strings = array(
+			'KERN', 'USER', 'MAIL', 'DAEMON', 'AUTH',
+			'SYSLOG', 'LPR', 'NEWS', 'UUCP', 'CRON', 'AUTHPRIV',
+			'LOCAL0', 'LOCAL1', 'LOCAL2', 'LOCAL3', 'LOCAL4',
+			'LOCAL5', 'LOCAL6', 'LOCAL7',
+		);
+		
+		// Only test facilities which exist on this OS
+		$facilities = array();
+		foreach($strings as $string) {
+			$const = "LOG_$string";
+			if (defined($const)) {
+				$facilities[$string] = constant($const); 
+			}
+		}
+		
+		// This makes reading of a private property possible
+		$property = new ReflectionProperty('LoggerAppenderSyslog', 'intFacility');
+		$property->setAccessible(true);
+	
+		// Check default value first
+		$appender = new LoggerAppenderSyslog();
+		$appender->activateOptions();
+		$actual = $property->getValue($appender);
+		$this->assertSame($default, $default, "Failed setting default facility [$defaultStr].");
+	
+		foreach($facilities as $facility => $expected) {
+			$appender = new LoggerAppenderSyslog();
+			$appender->setFacility($facility);
+			$appender->activateOptions();
+	
+			$actual = $property->getValue($appender);
+			$this->assertSame($expected, $actual, "Failed setting priority [$facility].");
+		}
+	}
+	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 */
+	public function testInvalidOption() {
+		$appender = new LoggerAppenderSyslog();
+		$appender->setOption('CONS|XYZ');
+		$appender->activateOptions();
+	}
+	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 */
+	public function testInvalidPriority() {
+		$appender = new LoggerAppenderSyslog();
+		$appender->setPriority('XYZ');
+		$appender->activateOptions();
+	}
+	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 */
+	public function testInvalidFacility() {
+		$appender = new LoggerAppenderSyslog();
+		$appender->setFacility('XYZ');
+		$appender->activateOptions();
+	}
+	
+	
+	public function testPriorityOverride() {
+		$appender = new LoggerAppenderSyslog();
+		$appender->setPriority('EMERG');
+		$appender->setOverridePriority(true);
+		$appender->activateOptions();
+		
+		$levels = array(
+			LoggerLevel::getLevelTrace(),
+			LoggerLevel::getLevelDebug(),
+			LoggerLevel::getLevelInfo(),
+			LoggerLevel::getLevelWarn(),
+			LoggerLevel::getLevelError(),
+			LoggerLevel::getLevelFatal(),
+		);
+		
+		$expected = LOG_EMERG;
+		
+		$method = new ReflectionMethod('LoggerAppenderSyslog', 'getSyslogPriority');
+		$method->setAccessible(true);
+		
+		foreach($levels as $level) {
+			$actual = $method->invoke($appender, $level);
+			$this->assertSame($expected, $actual);		
+		}
+	}
 }



Mime
View raw message