You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ih...@apache.org on 2011/09/17 09:06:34 UTC
svn commit: r1171923 - in
/logging/log4php/branches/experimental/config-adapters/src/main/php: ./
configurators/
Author: ihabunek
Date: Sat Sep 17 07:06:33 2011
New Revision: 1171923
URL: http://svn.apache.org/viewvc?rev=1171923&view=rev
Log:
Almost there.
Added:
logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php
- copied, changed from r1163006, logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php
Removed:
logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php
Modified:
logging/log4php/branches/experimental/config-adapters/src/main/php/Logger.php
logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerAppenderPool.php
logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterINI.php
logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterPHP.php
logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterXML.php
Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/Logger.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/Logger.php?rev=1171923&r1=1171922&r2=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/Logger.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/Logger.php Sat Sep 17 07:06:33 2011
@@ -53,7 +53,7 @@ class Logger {
'LoggerMDC' => '/LoggerMDC.php',
'LoggerNDC' => '/LoggerNDC.php',
'LoggerReflectionUtils' => '/LoggerReflectionUtils.php',
- 'LoggerConfigurator' => '/configurators/LoggerConfigurator.php',
+ 'LoggerConfigurator' => '/LoggerConfigurator.php',
'LoggerConfigurationAdapter' => '/configurators/LoggerConfigurationAdapter.php',
'LoggerConfigurationAdapterINI' => '/configurators/LoggerConfigurationAdapterINI.php',
'LoggerConfigurationAdapterXML' => '/configurators/LoggerConfigurationAdapterXML.php',
@@ -343,7 +343,7 @@ class Logger {
*/
public static function getLogger($name) {
if(!self::isInitialized()) {
- self::initialize();
+ self::configure();
}
return self::getHierarchy()->getLogger($name);
}
@@ -488,8 +488,6 @@ class Logger {
public static function resetConfiguration() {
$result = self::getHierarchy()->resetConfiguration();
self::$initialized = false;
- self::$configurationClass = 'LoggerConfiguratorBasic';
- self::$configurationFile = null;
return $result;
}
@@ -566,32 +564,15 @@ class Logger {
* should either be null or contain the path to file used by the custom
* configurator. Make sure the configurator class is already loaded, or
* that it can be included by PHP when necessary.
- *
- * @param string $configurationFile path to the configuration file
- * @param string $configurationClass name of the custom configurator class
*/
- public static function configure($configuration) {
+ public static function configure($configuration = null) {
if (!isset(self::$configurator)) {
self::$configurator = new LoggerConfigurator();
}
+ self::resetConfiguration();
self::$configurator->configure(self::getHierarchy(), $configuration);
- }
-
- /**
- * Returns the current {@link Logger::$configurationClass configurator class}.
- * @return string the configurator class name
- */
- public static function getConfigurationClass() {
- return self::$configurationClass;
- }
-
- /**
- * Returns the current {@link Logger::$configurationFile configuration file}.
- * @return string the configuration file
- */
- public static function getConfigurationFile() {
- return self::$configurationFile;
+ self::$initialized = true;
}
/**
@@ -602,16 +583,4 @@ class Logger {
return self::$initialized;
}
- /**
- * Initializes the log4php framework using the provided {@link
- * Logger::$configurationClass configuration class} and {@link
- * Logger::$configurationFile configuration file}.
- * @return boolean
- */
- public static function initialize() {
- self::$initialized = true;
- $instance = LoggerReflectionUtils::createObject(self::$configurationClass);
- $result = $instance->configure(self::getHierarchy(), self::$configurationFile);
- return $result;
- }
}
Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerAppenderPool.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerAppenderPool.php?rev=1171923&r1=1171922&r2=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerAppenderPool.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerAppenderPool.php Sat Sep 17 07:06:33 2011
@@ -25,36 +25,50 @@
* @package log4php
*/
class LoggerAppenderPool {
- /* Appender Pool */
- public static $appenderPool = array();
+ /** Holds appenders indexed by their name */
+ public static $appenders = array();
+
/**
- *
- *
- * @param string $name
- * @param string $class
- * @return LoggerAppender
+ * Adds an appender to the pool.
+ * The appender must be named for this operation.
+ * @param LoggerAppender $appender
*/
- public static function getAppenderFromPool($name, $class = '') {
- if(isset(self::$appenderPool[$name])) {
- return self::$appenderPool[$name];
- }
+ public static function add(LoggerAppender $appender)
+ {
+ $name = $appender->getName();
- if(empty($class)) {
- return null;
+ if(empty($name)) {
+ trigger_error('Cannot add unnamed appender to pool.', E_USER_WARNING);
+ return;
}
- $appender = LoggerReflectionUtils::createObject($class);
- $appender->setName($name);
- if($appender !== null) {
- self::$appenderPool[$name] = $appender;
- return self::$appenderPool[$name];
- }
- return null;
+ self::$appenders[$name] = $appender;
}
- public static function clear()
- {
- self::$appenderPool = array();
+ /**
+ * Retrieves an appender from the pool by name.
+ * @param string $name Name of the appender to retrieve.
+ * @return LoggerAppender The named appender or NULL if no such appender
+ * exists in the pool.
+ */
+ public static function get($name) {
+ return isset(self::$appenders[$name]) ? self::$appenders[$name] : null;
+ }
+
+ /**
+ * Checks whether an appender exists in the pool.
+ * @param string $name Name of the appender to look for.
+ * @return boolean TRUE if the appender with the given name exists.
+ */
+ public static function exists($name) {
+ return isset(self::$appenders[$name]);
+ }
+
+ /**
+ * Clears all appenders from the pool.
+ */
+ public static function clear() {
+ self::$appenders = array();
}
}
\ No newline at end of file
Copied: logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php (from r1163006, logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php)
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php?p2=logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php&p1=logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php&r1=1163006&r2=1171923&rev=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php Sat Sep 17 07:06:33 2011
@@ -19,7 +19,7 @@
*/
/**
- * Configures log4php based on a provided configuration file.
+ * Configures log4php based on a provided configuration file or array.
*/
class LoggerConfigurator
{
@@ -39,7 +39,7 @@ class LoggerConfigurator
self::FORMAT_PHP => 'LoggerConfigurationAdapterPHP',
);
- /** This configuration is used if no configuration file is provided. */
+ /** Default configuration; used if no configuration file is provided. */
private $defaultConfiguration = array(
'threshold' => 'ALL',
'rootLogger' => array(
@@ -70,22 +70,32 @@ class LoggerConfigurator
*/
public function configure(LoggerHierarchy $hierarchy, $input = null)
{
+ // No input - use default configuration
if (!isset($input)) {
$config = $this->defaultConfiguration;
}
+ // Array input - contains configuration within the array
else if (is_array($input)) {
$config = $input;
}
+ // String input - contains path to configuration file
else if (is_string($input)) {
- $config = $this->parseConfigFile($input);
+ try {
+ $config = $this->parseConfigFile($input);
+ } catch (Exception $e) {
+ $this->warn("Failed parsing configuration file: " . $e->getMessage());
+ $config = $this->defaultConfiguration;
+ }
}
+ // Anything else is an error
else {
- throw new LoggerException("Invalid configuration param: " . var_export($input));
+ $this->warn("Invalid configuration param given. Reverting to default configuration.");
+ $config = $this->defaultConfiguration;
}
- var_export($config);
+
$this->doConfigure($hierarchy, $config);
}
@@ -99,7 +109,7 @@ class LoggerConfigurator
* @throws LoggerException If the configuration file cannot be loaded, or
* if the parsing fails.
*/
- private function parseConfigFile($url)
+ public function parseConfigFile($url)
{
$type = $this->getConfigType($url);
$adapterClass = $this->adapters[$type];
@@ -136,109 +146,165 @@ class LoggerConfigurator
* @param array $config
*/
private function doConfigure(LoggerHierarchy $hierarchy, $config) {
-
if (isset($config['threshold'])) {
- $default = $hierarchy->getThreshold();
- $threshold = LoggerLevel::toLevel($config['threshold'], $default);
- $hierarchy->setThreshold($threshold);
+ $threshold = LoggerLevel::toLevel($config['threshold']);
+ if (isset($threshold)) {
+ $hierarchy->setThreshold($threshold);
+ } else {
+ $this->warn("Invalid threshold {$config['threshold']} specified.");
+ }
}
+ // Configure appenders and add them to the appender pool
if (isset($config['appenders']) && is_array($config['appenders'])) {
foreach($config['appenders'] as $name => $appenderConfig) {
$this->configureAppender($hierarchy, $name, $appenderConfig);
}
}
+ // Configure root logger
+ if (isset($config['rootLogger'])) {
+ $this->configureRootLogger($hierarchy, $config['rootLogger']);
+ }
+
+ // Configure loggers
if (isset($config['loggers']) && is_array($config['loggers'])) {
- foreach($config['loggers'] as $loggerConfig) {
- $this->configureLogger($hierarchy, $loggerConfig);
+ foreach($config['loggers'] as $loggerName => $loggerConfig) {
+ $this->configureOtherLogger($hierarchy, $loggerName, $loggerConfig);
}
}
-
- if (isset($config['rootLogger'])) {
- $this->configureRootLogger($hierarchy, $config['rootLogger']);
+
+ // Configure renderers
+ if (isset($config['renderers']) && is_array($config['renderers'])) {
+ foreach($config['renderers'] as $renderer) {
+ $hierarchy->getRendererMap()->addRenderer($renderer['renderedClass'], $renderer['renderingClass']);
+ }
}
}
-
private function configureAppender(LoggerHierarchy $hierarchy, $name, $config) {
$class = $config['class'];
if (!class_exists($class)) {
- $this->warn("Invalid appender configuration [$name]: class [$class] does not exist.");
+ $this->warn("Class [$class] does not exist. Skipping appender [$name].");
return;
}
- $appender = new $class();
-
+ $appender = new $class($name);
if (!($appender instanceof LoggerAppender)) {
- $this->warn("Invalid appender configuration [$name]: class [$class] is not a valid appender class.");
+ $this->warn("[$class] is not a valid appender class. Skipping appender [$name].");
return;
}
- $appender->setName($name);
-
+ // Parse the threshold
if (isset($config['threshold'])) {
- $default = $appender->getThreshold();
- $threshold = LoggerOptionConverter::toLevel($config['threshold'], $default);
- $appender->setThreshold($threshold);
+ $threshold = LoggerLevel::toLevel($config['threshold']);
+ if ($threshold instanceof LoggerLevel) {
+ $appender->setThreshold($threshold);
+ } else {
+ $default = $appender->getThreshold();
+ $this->warn("Invalid threshold value [{$config['threshold']}] specified for appender [$name]. Reverting to default value [$default].");
+ }
}
- if ($appender->requiresLayout()) {
- $layout = $this->createAppenderLayout($name, $config);
-
- // If layout doesn't exist or is wrongly defined, revert to default layout
- if (!isset($layout)) {
- $layout = new LoggerLayoutSimple();
- }
-
- $appender->setLayout($layout);
+ // Parse the appender layout
+ if ($appender->requiresLayout() && isset($config['layout'])) {
+ $this->createAppenderLayout($appender, $config['layout']);
}
+
+ $appender->activateOptions();
+
+ // Save appender in pool
+ LoggerAppenderPool::add($appender);
}
- private function createAppenderLayout($name, $config) {
- if (isset($config['layout'])) {
-
- $class = $config['layout']['class'];
-
- if (!class_exists($class)) {
- $this->warn("Appender [$name]: layout [$class] does not exist. Reverting to LoggerLayoutSimple.");
- return;
- }
-
- $layout = new $class;
-
- if (!($layout instanceof LoggerLayout)) {
- $this->warn("Appender [$name]: class [$class] is not a valid layout class. Reverting to LoggerLayoutSimple.");
- return;
- }
-
- return $layout;
+ /**
+ * Parses layout config, creates the layout and links it to the appender.
+ * @param LoggerAppender $appender
+ * @param array $config Layout configuration options.
+ */
+ private function createAppenderLayout(LoggerAppender $appender, $config) {
+ $name = $appender->getName();
+ $class = $config['class'];
+ if (!class_exists($class)) {
+ $this->warn("Nonexistant layout class [$class] specified for appender [$name]. Reverting to default layout.");
+ return;
}
- }
-
- private function configureLogger(LoggerHierarchy $hierarchy, $config) {
+ $layout = new $class;
+ if (!($layout instanceof LoggerLayout)) {
+ $this->warn("Invalid layout class [$class] sepcified for appender [$name]. Reverting to default layout.");
+ return;
+ }
+
+ unset($config['class']);
+ $this->setOptions($layout, $config);
+ $layout->activateOptions();
+
+ $appender->setLayout($layout);
}
private function configureRootLogger(LoggerHierarchy $hierarchy, $config) {
- $root = $hierarchy->getRootLogger();
+ $logger = $hierarchy->getRootLogger();
+ $this->configureLogger($logger, $config);
+ }
+
+ private function configureOtherLogger(LoggerHierarchy $hierarchy, $name, $config) {
+ // Get logger from hierarchy (this creates it if it doesn't already exist)
+ $logger = $hierarchy->getLogger($name);
+ $this->configureLogger($logger, $config);
+ }
+
+ private function configureLogger(Logger $logger, $config) {
+ $name = $logger->getName();
+ // Set logger level
if (isset($config['level'])) {
- $default = $root->getLevel();
- $level = LoggerOptionConverter::toLevel($config['level'], $default);
- $root->setLevel($level);
+ $level = LoggerLevel::toLevel($config['level']);
+ if (isset($level)) {
+ $logger->setLevel($level);
+ } else {
+ $this->warn("Invalid logger level [{$config['level']}] specified for logger [$name].");
+ }
}
+ // Link appenders to logger
if (isset($config['appenders'])) {
- foreach($config['appenders'] as $appenderRef) {
-
+ foreach($config['appenders'] as $appenderName) {
+ $appender = LoggerAppenderPool::get($appenderName);
+ if (isset($appender)) {
+ $logger->addAppender($appender);
+ } else {
+ $this->warn("Nonexistnant appender [$appenderName] linked to logger [$name].");
+ }
+ }
+ }
+
+ // Set logger additivity
+ if (isset($config['additivity'])) {
+ $additivity = LoggerOptionConverter::toBoolean($config['additivity'], null);
+ if (is_bool($additivity)) {
+ $logger->setAdditivity($additivity);
+ } else {
+ $this->warn("Invalid additivity value [{$config['additivity']}] specified for logger [$name].");
+ }
+ }
+ }
+
+ private function setOptions($object, $options) {
+ foreach($options as $name => $value) {
+ $setter = "set$name";
+ if (method_exists($object, $setter)) {
+ $object->$setter($value);
+ } else {
+ $class = get_class($object);
+ $this->warn("Nonexistant option [$name] specified on [$class]. Skipping.");
}
}
}
- private function warn($message)
- {
+ /** Helper method to simplify error reporting. */
+ private function warn($message) {
trigger_error("log4php: $message", E_USER_WARNING);
}
}
\ No newline at end of file
Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterINI.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterINI.php?rev=1171923&r1=1171922&r2=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterINI.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterINI.php Sat Sep 17 07:06:33 2011
@@ -4,14 +4,10 @@ class LoggerConfigurationAdapterINI impl
const ROOT_LOGGER_NAME = "root";
+ const ADDITIVITY_PREFIX = "log4php.additivity.";
const THRESHOLD_PREFIX = "log4php.threshold";
const ROOT_LOGGER_PREFIX = "log4php.rootLogger";
const LOGGER_PREFIX = "log4php.logger.";
-
-// const CATEGORY_PREFIX = "log4php.category.";
-// const FACTORY_PREFIX = "log4php.factory";
- const ADDITIVITY_PREFIX = "log4php.additivity.";
- const ROOT_CATEGORY_PREFIX = "log4php.rootCategory";
const APPENDER_PREFIX = "log4php.appender.";
const RENDERER_PREFIX = "log4php.renderer.";
@@ -71,13 +67,24 @@ class LoggerConfigurationAdapterINI impl
// Parse loggers
if ($this->beginsWith($key, self::LOGGER_PREFIX)) {
$name = substr($key, strlen(self::LOGGER_PREFIX));
- $this->parseLogger($property, $name);
+ $this->parseLogger($value, $name);
+ }
+
+ // Parse additivity
+ if ($this->beginsWith($key, self::ADDITIVITY_PREFIX)) {
+ $name = substr($key, strlen(self::ADDITIVITY_PREFIX));
+ $this->parseAdditivity($value, $name);
}
// Parse appenders
else if ($this->beginsWith($key, self::APPENDER_PREFIX)) {
$this->parseAppender($key, $value);
}
+
+ // Parse renderers
+ else if ($this->beginsWith($key, self::RENDERER_PREFIX)) {
+ $this->parseRenderer($key, $value);
+ }
}
return $this->config;
@@ -125,12 +132,27 @@ class LoggerConfigurationAdapterINI impl
);
if ($loggerName == self::ROOT_LOGGER_NAME) {
- $this->config['rootLogger'] = $config;
+ $target = &$this->config['rootLogger'];
+ } else {
+ $target = &$this->config['loggers'][$loggerName];
+ }
+
+ // It is possible that the logger config array already exists
+ // (set when parsing additivity) so make sure not to overwrite it.
+ if (is_array($target)) {
+ $target = array_merge($target, $config);
} else {
- $this->config['loggers'][$loggerName] = $config;
+ $target = $config;
}
}
+ private function parseAdditivity($value, $loggerName) {
+ if ($loggerName == self::ROOT_LOGGER_NAME) {
+ $this->config['rootLogger']['additivity'] = $value;
+ } else {
+ $this->config['loggers'][$loggerName]['additivity'] = $value;
+ }
+ }
/**
* Parses an configuration line pertaining to an appender.
@@ -209,6 +231,15 @@ class LoggerConfigurationAdapterINI impl
trigger_error("log4php: Error in config file \"$key = $value\". Skipped this line.");
}
+
+ private function parseRenderer($key, $value) {
+ // Remove the appender prefix from key
+ $renderedClass = substr($key, strlen(self::APPENDER_PREFIX));
+ $renderingClass = $value;
+
+ $this->config['renderers'][] = compact('renderedClass', 'renderingClass');
+ }
+
private function beginsWith($str, $sub) {
return (strncmp($str, $sub, strlen($sub)) == 0);
}
Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterPHP.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterPHP.php?rev=1171923&r1=1171922&r2=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterPHP.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterPHP.php Sat Sep 17 07:06:33 2011
@@ -28,7 +28,6 @@ class LoggerConfigurationAdapterPHP impl
return $config;
}
-
}
?>
\ No newline at end of file
Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterXML.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterXML.php?rev=1171923&r1=1171922&r2=1171923&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterXML.php (original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurationAdapterXML.php Sat Sep 17 07:06:33 2011
@@ -39,7 +39,6 @@ class LoggerConfigurationAdapterXML impl
foreach($xml->renderer as $rendererNode) {
$this->parseRenderer($rendererNode);
}
-
return $this->config;
}
@@ -48,26 +47,28 @@ class LoggerConfigurationAdapterXML impl
* @param string $url Input XML.
*/
private function loadXML($url) {
+ if (!file_exists($url)) {
+ $cwd = getcwd();
+ throw new LoggerException("Config file not found at [$url]. Current working dir is [$cwd].");
+ }
// Load the config file
$config = @file_get_contents($url);
if ($config === false) {
$error = error_get_last();
- throw new LoggerException("Cannot load config file: {$error['message']}");
+ throw new LoggerException("Cannot load config file.");
}
// Validate XML against schema
- $internal = libxml_use_internal_errors(true);
-
- $this->validateXML($config);
-
- libxml_clear_errors();
- libxml_use_internal_errors($internal);
+// $internal = libxml_use_internal_errors(true);
+// $this->validateXML($config);
+// libxml_clear_errors();
+// libxml_use_internal_errors($internal);
// Load XML
$xml = simplexml_load_string($config);
if ($xml === false) {
- throw new LoggerException("Failed parsing XML configuration file.");
+ throw new LoggerException("XML file contains errors.");
}
return $xml;
}
@@ -164,8 +165,6 @@ class LoggerConfigurationAdapterXML impl
private function parseRootLogger(SimpleXMLElement $node) {
$logger = array();
- var_dump($node->level['value']);
-
if (isset($node->level)) {
$logger['level'] = $this->getAttributeValue($node->level, 'value');
}