loggerdomconfigurator.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 612 行 · 第 1/2 页

PHP
612
字号
<?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.
 *
 *
 * @package log4php
 * @subpackage xml
 */

/**
 * @ignore 
 */
if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
 
require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
require_once(LOG4PHP_DIR . '/or/LoggerObjectRenderer.php');
require_once(LOG4PHP_DIR . '/spi/LoggerConfigurator.php');
require_once(LOG4PHP_DIR . '/LoggerAppender.php');
require_once(LOG4PHP_DIR . '/LoggerLayout.php');
require_once(LOG4PHP_DIR . '/LoggerLog.php');
require_once(LOG4PHP_DIR . '/LoggerManager.php');

define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_APPENDER_STATE',    1000);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_LAYOUT_STATE',      1010);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE',        1020);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE',      1030);
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_FILTER_STATE',      1040);

define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_FILENAME',  './log4php.xml');

/**
 * @var string the default configuration document
 */
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_CONFIGURATION', 
'<?xml version="1.0" ?>
<log4php:configuration threshold="all">
    <appender name="A1" class="LoggerAppenderEcho">
        <layout class="LoggerLayoutSimple" />
    </appender>
    <root>
        <level value="debug" />
        <appender_ref ref="A1" />
    </root>
</log4php:configuration>');

/**
 * @var string the elements namespace
 */
define('LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS', 'HTTP://LOGGING.APACHE.ORG/LOG4PHP/'); 

/**
 * Use this class to initialize the log4php environment using expat parser.
 *
 * <p>Read the log4php.dtd included in the documentation directory. Note that
 * php parser does not validate the document.</p>
 *
 * <p>Sometimes it is useful to see how log4php is reading configuration
 * files. You can enable log4php internal logging by setting the <var>debug</var> 
 * attribute in the <var>log4php:configuration</var> element. As in
 * <pre>
 * &lt;log4php:configuration <b>debug="true"</b> xmlns:log4php="http://logging.apache.org/log4php/">
 * ...
 * &lt;/log4php:configuration>
 * </pre>
 *
 * <p>There are sample XML files included in the package under <b>tests/</b> 
 * subdirectories.</p>
 *
 * @author  Marco Vassura
 * @author Knut Urdalen <knut.urdalen@gmail.com>
 * @version $Revision: 635069 $
 * @package log4php
 * @subpackage xml
 * @since 0.4 
 */
class LoggerDOMConfigurator implements LoggerConfigurator {

    /**
     * @var LoggerHierarchy
     */
    var $repository;
    
    /**
     * @var array state stack 
     */
    var $state;

    /**
     * @var Logger parsed Logger  
     */
    var $logger;
    
    /**
     * @var LoggerAppender parsed LoggerAppender 
     */
    var $appender;
    
    /**
     * @var LoggerFilter parsed LoggerFilter 
     */
    var $filter;
    
    /**
     * @var LoggerLayout parsed LoggerLayout 
     */
    var $layout;
    
    /**
     * Constructor
     */
    function LoggerDOMConfigurator()
    {
        $this->state    = array();
        $this->logger   = null;
        $this->appender = null;
        $this->filter   = null;
        $this->layout   = null;
    }
    
    /**
     * Configure the default repository using the resource pointed by <b>url</b>.
     * <b>Url</b> is any valid resource as defined in {@link PHP_MANUAL#file} function.
     * Note that the resource will be search with <i>use_include_path</i> parameter 
     * set to "1".
     *
     * @param string $url
     * @static
     */
    public static function configure($url = '') {
        $configurator = new LoggerDOMConfigurator();
        $repository =& LoggerManager::getLoggerRepository();
        return $configurator->doConfigure($url, $repository);
    }
    
    /**
     * Configure the given <b>repository</b> using the resource pointed by <b>url</b>.
     * <b>Url</b> is any valid resurce as defined in {@link PHP_MANUAL#file} function.
     * Note that the resource will be search with <i>use_include_path</i> parameter 
     * set to "1".
     *
     * @param string $url
     * @param LoggerHierarchy &$repository
     */
    function doConfigure($url = '', &$repository)
    {
        $xmlData = '';
        if (!empty($url))
            $xmlData = implode('', file($url, 1));
        return $this->doConfigureByString($xmlData, $repository);
    }
    
    /**
     * Configure the given <b>repository</b> using the configuration written in <b>xmlData</b>.
     * Do not call this method directly. Use {@link doConfigure()} instead.
     * @param string $xmlData
     * @param LoggerHierarchy &$repository
     */
    function doConfigureByString($xmlData, &$repository)
    {
        return $this->parse($xmlData, $repository);
    }
    
    /**
     * @param LoggerHierarchy &$repository
     */
    function doConfigureDefault(&$repository)
    {
        return $this->doConfigureByString(LOG4PHP_LOGGER_DOM_CONFIGURATOR_DEFAULT_CONFIGURATION, $repository);
    }
    
    /**
     * @param string $xmlData
     */
    function parse($xmlData, &$repository)
    {
        // LoggerManager::resetConfiguration();
        $this->repository =& $repository;

        $parser = xml_parser_create_ns();
    
        xml_set_object($parser, $this);
        xml_set_element_handler($parser, "tagOpen", "tagClose");
        
        $result = xml_parse($parser, $xmlData, true);
        if (!$result) {
            $errorCode = xml_get_error_code($parser);
            $errorStr = xml_error_string($errorCode);
            $errorLine = xml_get_current_line_number($parser);
            LoggerLog::warn(
                "LoggerDOMConfigurator::parse() ".
                "Parsing error [{$errorCode}] {$errorStr}, line {$errorLine}"
            );
            $this->repository->resetConfiguration();
        } else {
            xml_parser_free($parser);
        }
        return $result;
    }
    
    /**
     * @param mixed $parser
     * @param string $tag
     * @param array $attribs
     *
     * @todo In 'LOGGER' case find a better way to detect 'getLogger()' method
     */
    function tagOpen($parser, $tag, $attribs)
    {
        switch ($tag) {
        
            case 'CONFIGURATION' :
            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':CONFIGURATION':
            
                LoggerLog::debug("LoggerDOMConfigurator::tagOpen() CONFIGURATION");

                if (isset($attribs['THRESHOLD'])) {
                
                    $this->repository->setThreshold(
                        LoggerOptionConverter::toLevel(
                            $this->subst($attribs['THRESHOLD']), 
                            $this->repository->getThreshold()
                        )
                    );
                }
                if (isset($attribs['DEBUG'])) {
                    $debug = LoggerOptionConverter::toBoolean($this->subst($attribs['DEBUG']), LoggerLog::internalDebugging());
                    $this->repository->debug = $debug;
                    LoggerLog::internalDebugging($debug);
                    LoggerLog::debug("LoggerDOMConfigurator::tagOpen() LOG4PHP:CONFIGURATION. Internal Debug turned ".($debug ? 'on':'off'));
                    
                }
                break;
                
            case 'APPENDER' :
            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER':
            
                unset($this->appender);
                $this->appender = null;
                
                $name  = $this->subst(@$attribs['NAME']);
                $class = $this->subst(@$attribs['CLASS']);
                
                LoggerLog::debug("LoggerDOMConfigurator::tagOpen():tag=[$tag]:name=[$name]:class=[$class]");
                
                $this->appender =& LoggerAppender::singleton($name, $class);
                if ($this->appender === null) {
                    LoggerLog::warn("LoggerDOMConfigurator::tagOpen() APPENDER cannot instantiate appender '$name'");
                }
                $this->state[] = LOG4PHP_LOGGER_DOM_CONFIGURATOR_APPENDER_STATE;
                break;
                
            case 'APPENDER_REF' :
            case 'APPENDER-REF' :
            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER_REF':
            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':APPENDER-REF':
            
            
                if (isset($attribs['REF']) and !empty($attribs['REF'])) {
                    $appenderName = $this->subst($attribs['REF']);
                    
                    LoggerLog::debug("LoggerDOMConfigurator::tagOpen() APPENDER-REF ref='$appenderName'");        
                    
                    $appender =& LoggerAppender::singleton($appenderName);
                    if ($appender !== null) {
                        switch (end($this->state)) {
                            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_LOGGER_STATE:
                            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_ROOT_STATE:                
                                $this->logger->addAppender($appender);
                                break;
                        }
                    } else {
                        LoggerLog::warn("LoggerDOMConfigurator::tagOpen() APPENDER-REF ref '$appenderName' points to a null appender");
                    }
                } else {
                    LoggerLog::warn("LoggerDOMConfigurator::tagOpen() APPENDER-REF ref not set or empty");            
                }
                break;
                
            case 'FILTER' :
            case LOG4PHP_LOGGER_DOM_CONFIGURATOR_XMLNS.':FILTER':
            
                LoggerLog::debug("LoggerDOMConfigurator::tagOpen() FILTER");
                            
                unset($this->filter);
                $this->filter = null;

                $filterName = basename($this->subst(@$attribs['CLASS']));
                if (!empty($filterName)) {
                    if (!class_exists($filterName)) {
                        @include_once(LOG4PHP_DIR . "/varia/{$filterName}.php");
                    }
                    if (class_exists($filterName)) {
                        $this->filter = new $filterName();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?