You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4php-dev@logging.apache.org by gr...@apache.org on 2009/08/28 07:55:52 UTC
svn commit: r808763 - in /incubator/log4php/trunk/src:
main/php/appenders/LoggerAppenderSyslog.php
test/php/appenders/LoggerAppenderSyslogTest.php
Author: grobmeier
Date: Fri Aug 28 05:55:52 2009
New Revision: 808763
URL: http://svn.apache.org/viewvc?rev=808763&view=rev
Log:
introduced "dry mode" to enable unit tests
Added:
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php
Modified:
incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php
Modified: incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php
URL: http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php?rev=808763&r1=808762&r2=808763&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php (original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSyslog.php Fri Aug 28 05:55:52 2009
@@ -73,6 +73,9 @@
*/
private $_overridePriority;
+ /** @var indiciates if this appender should run in dry mode */
+ private $dry = false;
+
public function __construct($name = '') {
parent::__construct($name);
$this->requiresLayout = true;
@@ -82,6 +85,10 @@
$this->close();
}
+ public function setDry($dry) {
+ $this->dry = $dry;
+ }
+
/**
* Set the ident of the syslog message.
*
@@ -132,7 +139,8 @@
}
public function activateOptions() {
- define_syslog_variables();
+ // Deprecated as of 5.3 and removed in 6.0
+ // define_syslog_variables();
$this->closed = false;
}
@@ -148,9 +156,6 @@
$this->_option = LOG_PID | LOG_CONS;
}
- // Attach the process ID to the message, use the facility defined in the .properties-file
- openlog($this->_ident, $this->_option, $this->_facility);
-
$level = $event->getLevel();
if($this->layout === null) {
$message = $event->getRenderedMessage();
@@ -160,21 +165,28 @@
// 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->_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);
+ 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);
+ }
}
+ closelog();
+ } else {
+ echo "DRY MODE OF SYSLOG APPENDER: ".$message;
}
- closelog();
}
}
Added: incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php
URL: http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php?rev=808763&view=auto
==============================================================================
--- incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php (added)
+++ incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSyslogTest.php Fri Aug 28 05:55:52 2009
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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
+ * @package log4php
+ * @subpackage appenders
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
+ * @version SVN: $Id$
+ * @link http://logging.apache.org/log4php
+ */
+
+class LoggerAppenderSyslogTest extends PHPUnit_Framework_TestCase {
+
+ public function testSyslog() {
+ $appender = new LoggerAppenderSyslog("myname ");
+
+ $layout = new LoggerLayoutSimple();
+ $appender->setLayout($layout);
+ $appender->setDry(true);
+ $appender->activateOptions();
+ $event = new LoggerLoggingEvent("LoggerAppenderSyslogTest", new Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
+
+ ob_start();
+ $appender->append($event);
+ $v = ob_get_contents();
+ ob_end_clean();
+
+ $e = "DRY MODE OF SYSLOG APPENDER: ERROR - testmessage".PHP_EOL;
+ self::assertEquals($v, $e);
+ }
+
+}