From commits-return-1-apmail-logging-commits-archive=logging.apache.org@logging.apache.org Mon Aug 29 21:09:16 2011 Return-Path: X-Original-To: apmail-logging-commits-archive@minotaur.apache.org Delivered-To: apmail-logging-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7756E7E48 for ; Mon, 29 Aug 2011 21:09:16 +0000 (UTC) Received: (qmail 19176 invoked by uid 500); 29 Aug 2011 21:09:16 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 19154 invoked by uid 500); 29 Aug 2011 21:09:16 -0000 Mailing-List: contact commits-help@logging.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@logging.apache.org Delivered-To: mailing list commits@logging.apache.org Received: (qmail 19146 invoked by uid 99); 29 Aug 2011 21:09:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Aug 2011 21:09:16 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Aug 2011 21:09:12 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 638C52388A5F; Mon, 29 Aug 2011 21:08:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1163006 - /logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php Date: Mon, 29 Aug 2011 21:08:51 -0000 To: commits@logging.apache.org From: ihabunek@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110829210851.638C52388A5F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ihabunek Date: Mon Aug 29 21:08:51 2011 New Revision: 1163006 URL: http://svn.apache.org/viewvc?rev=1163006&view=rev Log: Initial implementation of the Configurator. Added: logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php Added: 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/configurators/LoggerConfigurator.php?rev=1163006&view=auto ============================================================================== --- logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php (added) +++ logging/log4php/branches/experimental/config-adapters/src/main/php/configurators/LoggerConfigurator.php Mon Aug 29 21:08:51 2011 @@ -0,0 +1,244 @@ + 'LoggerConfigurationAdapterXML', + self::FORMAT_INI => 'LoggerConfigurationAdapterINI', + self::FORMAT_PHP => 'LoggerConfigurationAdapterPHP', + ); + + /** This configuration is used if no configuration file is provided. */ + private $defaultConfiguration = array( + 'threshold' => 'ALL', + 'rootLogger' => array( + 'level' => 'INFO', + 'appenders' => array('default'), + ), + 'appenders' => array( + 'default' => array( + 'class' => 'LoggerAppenderEcho', + 'layout' => array( + 'class' => 'LoggerLayoutSimple', + ), + ), + ), + ); + + /** + * Starts logger configuration procedure. + * + * If the config file cannot be loaded or parsed, reverts to the default + * configuration contained in {@link $defaultConfiguration}. + * + * @param LoggerHierarchy $hierarchy The hierarchy on which to perform + * the configuration. + * @param string|array $input Either path to the config file or the + * configuration as an array. If not set, default configuration + * will be used. + */ + public function configure(LoggerHierarchy $hierarchy, $input = null) + { + if (!isset($input)) { + $config = $this->defaultConfiguration; + } + + else if (is_array($input)) { + $config = $input; + } + + else if (is_string($input)) { + $config = $this->parseConfigFile($input); + } + + else { + throw new LoggerException("Invalid configuration param: " . var_export($input)); + } + var_export($config); + $this->doConfigure($hierarchy, $config); + } + + /** + * Loads the configuration file from the given URL, determines which + * adapter to use, converts the configuration to a PHP array and + * returns it. + * + * @param string $url Path to the config file. + * @return The configuration from the config file, as a PHP array. + * @throws LoggerException If the configuration file cannot be loaded, or + * if the parsing fails. + */ + private function parseConfigFile($url) + { + $type = $this->getConfigType($url); + $adapterClass = $this->adapters[$type]; + + $adapter = new $adapterClass(); + return $adapter->convert($url); + } + + /** Determines configuration file type based on the file extension. */ + private function getConfigType($url) { + $info = pathinfo($url); + $ext = strtolower($info['extension']); + + switch($ext) { + case 'xml': + return self::FORMAT_XML; + + case 'ini': + case 'properties': + return self::FORMAT_INI; + + case 'php': + return self::FORMAT_PHP; + + default: + throw new LoggerException("Unsupported configuration file extension: $ext"); + } + } + + /** + * Constructs the logger hierarchy based on configuration. + * + * @param LoggerHierarchy $hierarchy + * @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); + } + + if (isset($config['appenders']) && is_array($config['appenders'])) { + foreach($config['appenders'] as $name => $appenderConfig) { + $this->configureAppender($hierarchy, $name, $appenderConfig); + } + } + + if (isset($config['loggers']) && is_array($config['loggers'])) { + foreach($config['loggers'] as $loggerConfig) { + $this->configureLogger($hierarchy, $loggerConfig); + } + } + + if (isset($config['rootLogger'])) { + $this->configureRootLogger($hierarchy, $config['rootLogger']); + } + } + + + 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."); + return; + } + + $appender = new $class(); + + if (!($appender instanceof LoggerAppender)) { + $this->warn("Invalid appender configuration [$name]: class [$class] is not a valid appender class."); + return; + } + + $appender->setName($name); + + if (isset($config['threshold'])) { + $default = $appender->getThreshold(); + $threshold = LoggerOptionConverter::toLevel($config['threshold'], $default); + $appender->setThreshold($threshold); + } + + 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); + } + } + + 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; + } + } + + private function configureLogger(LoggerHierarchy $hierarchy, $config) { + + } + + private function configureRootLogger(LoggerHierarchy $hierarchy, $config) { + $root = $hierarchy->getRootLogger(); + + if (isset($config['level'])) { + $default = $root->getLevel(); + $level = LoggerOptionConverter::toLevel($config['level'], $default); + $root->setLevel($level); + } + + if (isset($config['appenders'])) { + foreach($config['appenders'] as $appenderRef) { + + } + } + } + + private function warn($message) + { + trigger_error("log4php: $message", E_USER_WARNING); + } +} \ No newline at end of file