/**
  * @expectedException PHPUnit_Framework_Error
  * @expectedExceptionMessage log4php: Appender [foo] already exists in pool. Overwriting existing appender.
  */
 public function testAppenderIsAdded()
 {
     $this->appenderMock->expects($this->any())->method('getName')->will($this->returnValue('foo'));
     LoggerAppenderPool::add($this->appenderMock);
     LoggerAppenderPool::add($this->appenderMock);
     $expected = 1;
     $actual = count(LoggerAppenderPool::getAppenders());
     $this->assertEquals($expected, $actual);
 }
Beispiel #2
0
 /**
  * Reset all values contained in this hierarchy instance to their
  * default. 
  *
  * This removes all appenders from all loggers, sets
  * the level of all non-root loggers to <i>null</i>,
  * sets their additivity flag to <i>true</i> and sets the level
  * of the root logger to {@link LOGGER_LEVEL_DEBUG}.
  * 
  * <p>Existing loggers are not removed. They are just reset.
  *
  * <p>This method should be used sparingly and with care as it will
  * block all logging until it is completed.</p>
  */
 public function resetConfiguration()
 {
     $root = $this->getRootLogger();
     $root->setLevel(LoggerLevel::getLevelDebug());
     $this->setThreshold(LoggerLevel::getLevelAll());
     $this->shutDown();
     foreach ($this->loggers as $logger) {
         $logger->setLevel(null);
         $logger->setAdditivity(true);
         $logger->removeAllAppenders();
     }
     $this->rendererMap->reset();
     LoggerAppenderPool::clear();
 }
 /**
  * @param mixed $parser
  * @param string $tag
  * @param array $attribs
  *
  * @todo In 'LOGGER' case find a better way to detect 'getLogger()' method
  */
 private function tagOpen($parser, $tag, $attribs)
 {
     switch ($tag) {
         case 'CONFIGURATION':
         case self::XMLNS . ':CONFIGURATION':
             if (isset($attribs['THRESHOLD'])) {
                 $this->repository->setThreshold(LoggerOptionConverter::toLevel($this->subst($attribs['THRESHOLD']), $this->repository->getThreshold()));
             }
             break;
         case 'APPENDER':
         case self::XMLNS . ':APPENDER':
             unset($this->appender);
             $this->appender = null;
             $name = $this->subst(@$attribs['NAME']);
             $class = $this->subst(@$attribs['CLASS']);
             $this->appender = LoggerAppenderPool::getAppenderFromPool($name, $class);
             if (isset($attribs['THRESHOLD'])) {
                 $this->appender->setThreshold(LoggerOptionConverter::toLevel($this->subst($attribs['THRESHOLD']), $this->appender->getThreshold()));
             }
             $this->state[] = self::APPENDER_STATE;
             break;
         case 'APPENDER_REF':
         case 'APPENDER-REF':
         case self::XMLNS . ':APPENDER_REF':
         case self::XMLNS . ':APPENDER-REF':
             if (isset($attribs['REF']) and !empty($attribs['REF'])) {
                 $appenderName = $this->subst($attribs['REF']);
                 $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                 if ($appender !== null) {
                     switch (end($this->state)) {
                         case self::LOGGER_STATE:
                         case self::ROOT_STATE:
                             $this->logger->addAppender($appender);
                             break;
                     }
                 }
             }
             break;
         case 'FILTER':
         case self::XMLNS . ':FILTER':
             unset($this->filter);
             $this->filter = null;
             $filterName = basename($this->subst(@$attribs['CLASS']));
             if (!empty($filterName)) {
                 $this->filter = new $filterName();
                 $this->state[] = self::FILTER_STATE;
             }
             break;
         case 'LAYOUT':
         case self::XMLNS . ':LAYOUT':
             $class = @$attribs['CLASS'];
             $this->layout = LoggerReflectionUtils::createObject($this->subst($class));
             $this->state[] = self::LAYOUT_STATE;
             break;
         case 'LOGGER':
         case self::XMLNS . ':LOGGER':
             // $this->logger is assigned by reference.
             // Only '$this->logger=null;' destroys referenced object
             unset($this->logger);
             $this->logger = null;
             $loggerName = $this->subst(@$attribs['NAME']);
             if (!empty($loggerName)) {
                 $this->logger = $this->repository->getLogger($loggerName);
                 if ($this->logger !== null and isset($attribs['ADDITIVITY'])) {
                     $additivity = LoggerOptionConverter::toBoolean($this->subst($attribs['ADDITIVITY']), true);
                     $this->logger->setAdditivity($additivity);
                 }
             }
             $this->state[] = self::LOGGER_STATE;
             break;
         case 'LEVEL':
         case self::XMLNS . ':LEVEL':
         case 'PRIORITY':
         case self::XMLNS . ':PRIORITY':
             if (!isset($attribs['VALUE'])) {
                 // LoggerDOMConfigurator::tagOpen() LEVEL value not set
                 break;
             }
             if ($this->logger === null) {
                 // LoggerDOMConfigurator::tagOpen() LEVEL. parent logger is null
                 break;
             }
             switch (end($this->state)) {
                 case self::ROOT_STATE:
                     $this->logger->setLevel(LoggerOptionConverter::toLevel($this->subst($attribs['VALUE']), $this->logger->getLevel()));
                     break;
                 case self::LOGGER_STATE:
                     $this->logger->setLevel(LoggerOptionConverter::toLevel($this->subst($attribs['VALUE']), $this->logger->getLevel()));
                     break;
                 default:
                     //LoggerLog::warn("LoggerDOMConfigurator::tagOpen() LEVEL state '{$this->state}' not allowed here");
             }
             break;
         case 'PARAM':
         case self::XMLNS . ':PARAM':
             if (!isset($attribs['NAME'])) {
                 // LoggerDOMConfigurator::tagOpen() PARAM attribute 'name' not defined.
                 break;
             }
             if (!isset($attribs['VALUE'])) {
                 // LoggerDOMConfigurator::tagOpen() PARAM. attribute 'value' not defined.
                 break;
             }
             switch (end($this->state)) {
                 case self::APPENDER_STATE:
                     if ($this->appender !== null) {
                         LoggerReflectionUtils::setter($this->appender, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
                     }
                     break;
                 case self::LAYOUT_STATE:
                     if ($this->layout !== null) {
                         LoggerReflectionUtils::setter($this->layout, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
                     }
                     break;
                 case self::FILTER_STATE:
                     if ($this->filter !== null) {
                         LoggerReflectionUtils::setter($this->filter, $this->subst($attribs['NAME']), $this->subst($attribs['VALUE']));
                     }
                     break;
                 default:
                     //LoggerLog::warn("LoggerDOMConfigurator::tagOpen() PARAM state '{$this->state}' not allowed here");
             }
             break;
         case 'RENDERER':
         case self::XMLNS . ':RENDERER':
             $renderedClass = $this->subst(@$attribs['RENDEREDCLASS']);
             $renderingClass = $this->subst(@$attribs['RENDERINGCLASS']);
             if (!empty($renderedClass) and !empty($renderingClass)) {
                 $this->repository->getRendererMap()->addRenderer($renderedClass, $renderingClass);
             }
             break;
         case 'ROOT':
         case self::XMLNS . ':ROOT':
             $this->logger = Logger::getRootLogger();
             $this->state[] = self::ROOT_STATE;
             break;
     }
 }
Beispiel #4
0
 /**
  * Reset all values contained in this hierarchy instance to their
  * default. 
  *
  * This removes all appenders From all categories, sets
  * the level of all non-root categories to <i>null</i>,
  * sets their additivity flag to <i>true</i> and sets the level
  * of the root logger to {@link LOGGER_LEVEL_DEBUG}.  Moreover,
  * message disabling is set its default "off" value.
  * 
  * <p>Existing categories are not removed. They are just reset.
  *
  * <p>This method should be used sparingly and with care as it will
  * block all logging until it is completed.</p>
  */
 public function resetConfiguration()
 {
     $root = $this->getRootLogger();
     $root->setLevel(LoggerLevel::getLevelDebug());
     $this->setThreshold(LoggerLevel::getLevelAll());
     $this->shutDown();
     $loggers = $this->getCurrentLoggers();
     $enumLoggers = count($loggers);
     for ($i = 0; $i < $enumLoggers; $i++) {
         $loggers[$i]->setLevel(null);
         $loggers[$i]->setAdditivity(true);
         $loggers[$i]->removeAllAppenders();
     }
     $this->rendererMap->clear();
     LoggerAppenderPool::clear();
 }
 /**
  * @param array $props array of properties
  * @param string $appenderName
  * @return LoggerAppender
  */
 private function parseAppender($props, $appenderName)
 {
     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
     $prefix = self::APPENDER_PREFIX . $appenderName;
     if ($appender === null) {
         // Appender was not previously initialized.
         $appenderClass = @$props[$prefix];
         $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderClass);
         if ($appender === null) {
             return null;
         }
     }
     if ($appender->requiresLayout()) {
         $layoutPrefix = $prefix . ".layout";
         $layoutClass = @$props[$layoutPrefix];
         $layoutClass = LoggerOptionConverter::substVars($layoutClass, $props);
         if (empty($layoutClass)) {
             $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
         } else {
             $layout = LoggerReflectionUtils::createObject($layoutClass);
             if ($layout === null) {
                 $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
             }
         }
         LoggerReflectionUtils::setPropertiesByObject($layout, $props, $layoutPrefix . ".");
         $appender->setLayout($layout);
     }
     LoggerReflectionUtils::setPropertiesByObject($appender, $props, $prefix . ".");
     return $appender;
 }
 /**
  * Clears all appenders from the pool.
  */
 public static function clear()
 {
     self::$appenders = array();
 }
 private function doConfigure($url, LoggerHierarchy $hierarchy)
 {
     $config = (require $url);
     // set threshold
     if (isset($config['threshold'])) {
         $hierarchy->setThreshold(LoggerOptionConverter::toLevel($config['threshold'], LoggerLevel::getLevelAll()));
     }
     // parse and create appenders
     if (isset($config['appenders'])) {
         foreach ($config['appenders'] as $appenderName => $appenderProperties) {
             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderProperties['class']);
             if ($appender->requiresLayout()) {
                 if (isset($appenderProperties['layout'])) {
                     if (isset($appenderProperties['layout']['class']) and !empty($appenderProperties['layout']['class'])) {
                         $layoutClass = $appenderProperties['layout']['class'];
                     } else {
                         $layoutClass = 'LoggerLayoutSimple';
                     }
                     $layout = LoggerReflectionUtils::createObject($layoutClass);
                     if ($layout === null) {
                         $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
                     }
                     if ($layout instanceof LoggerLayoutPattern) {
                         $layout->setConversionPattern($appenderProperties['layout']['conversionPattern']);
                     }
                     $appender->setLayout($layout);
                 } else {
                     // TODO: throw exception?
                 }
             }
         }
     }
     // parse and create root logger
     if (isset($config['rootLogger'])) {
         $rootLogger = $hierarchy->getRootLogger();
         if (isset($config['rootLogger']['level'])) {
             $rootLogger->setLevel(LoggerOptionConverter::toLevel($config['rootLogger']['level'], LoggerLevel::getLevelDebug()));
             if (isset($config['rootLogger']['appenders'])) {
                 foreach ($config['rootLogger']['appenders'] as $appenderName) {
                     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                     if ($appender !== null) {
                         $rootLogger->addAppender($appender);
                     }
                 }
             }
         }
     }
     // parse and create loggers
     if (isset($config['loggers'])) {
         foreach ($config['loggers'] as $loggerName => $loggerProperties) {
             if (is_string($loggerName)) {
                 $logger = $hierarchy->getLogger($loggerName);
                 if (isset($loggerProperties['level'])) {
                     $logger->setLevel(LoggerOptionConverter::toLevel($loggerProperties['level'], LoggerLevel::getLevelDebug()));
                     if (isset($loggerProperties['appenders'])) {
                         foreach ($loggerProperties['appenders'] as $appenderName) {
                             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                             if ($appender !== null) {
                                 $logger->addAppender($appender);
                             }
                         }
                     }
                 }
             } else {
                 // TODO: throw exception
             }
         }
     }
     return true;
 }
 private function doConfigure($url, LoggerHierarchy $hierarchy)
 {
     if (!is_array($url)) {
         $config = (require $url);
     } else {
         $config = $url;
     }
     // set threshold
     if (isset($config['threshold'])) {
         $hierarchy->setThreshold(LoggerOptionConverter::toLevel($config['threshold'], LoggerLevel::getLevelAll()));
     }
     // add renderes
     if (isset($config['renderers'])) {
         foreach ($config['renderers'] as $renderedClass => $renderingClass) {
             $hierarchy->getRendererMap()->addRenderer($renderedClass, $renderingClass);
         }
     }
     // parse and create appenders
     if (isset($config['appenders'])) {
         foreach ($config['appenders'] as $appenderName => $appenderProperties) {
             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderProperties['class']);
             // unset so that the property wont be drawn up again
             unset($appenderProperties['class']);
             if ($appender->requiresLayout()) {
                 if (isset($appenderProperties['layout'])) {
                     if (isset($appenderProperties['layout']['class']) and !empty($appenderProperties['layout']['class'])) {
                         $layoutClass = $appenderProperties['layout']['class'];
                     } else {
                         $layoutClass = 'LoggerLayoutSimple';
                     }
                     $layout = LoggerReflectionUtils::createObject($layoutClass);
                     if ($layout === null) {
                         $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
                     }
                     if (isset($appenderProperties['file']) && method_exists($appender, 'setFileName')) {
                         $appender->setFile($appenderProperties['file'], true);
                     }
                     if ($layout instanceof LoggerLayoutPattern) {
                         $layout->setConversionPattern($appenderProperties['layout']['conversionPattern']);
                     }
                     $appender->setLayout($layout);
                     // unset so that the property wont be drawn up again
                     unset($appenderProperties['layout']);
                 } else {
                     // TODO: throw exception?
                 }
             }
             // set remaining properties and activate appender
             $setter = new LoggerReflectionUtils($appender);
             foreach ($appenderProperties as $key => $val) {
                 $setter->setProperty($key, $val);
             }
             $setter->activate();
         }
     }
     // parse and create root logger
     if (isset($config['rootLogger'])) {
         $rootLogger = $hierarchy->getRootLogger();
         if (isset($config['rootLogger']['level'])) {
             $rootLogger->setLevel(LoggerOptionConverter::toLevel($config['rootLogger']['level'], LoggerLevel::getLevelDebug()));
             if (isset($config['rootLogger']['appenders'])) {
                 foreach ($config['rootLogger']['appenders'] as $appenderName) {
                     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                     if ($appender !== null) {
                         $rootLogger->addAppender($appender);
                     }
                 }
             }
         }
     }
     // parse and create loggers
     if (isset($config['loggers'])) {
         foreach ($config['loggers'] as $loggerName => $loggerProperties) {
             if (is_string($loggerName)) {
                 $logger = $hierarchy->getLogger($loggerName);
                 if (isset($loggerProperties['level'])) {
                     $logger->setLevel(LoggerOptionConverter::toLevel($loggerProperties['level'], LoggerLevel::getLevelDebug()));
                     if (isset($loggerProperties['appenders'])) {
                         foreach ($loggerProperties['appenders'] as $appenderName) {
                             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                             if ($appender !== null) {
                                 $logger->addAppender($appender);
                             }
                         }
                     }
                 }
             } else {
                 // TODO: throw exception
             }
         }
     }
     return true;
 }