LoggerConfiguratorXml.php 15.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
<?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
 */

/**
 * Use this class to initialize the log4php environment using XML files.
 *
 * <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.</p>
 *
 * <p>An example for this configurator:</p>
 * 
 * {@example ../../examples/php/configurator_xml.php 19}<br>
 * 
 * <p>The corresponding XML file:</p>
 * 
 * {@example ../../examples/resources/configurator_xml.xml 18}
 * 
 * <p>There are more sample XML files included in the package under tests/ subdirectories.</p>
 *
 * @version $Revision: 883108 $
 * @package log4php
 * @subpackage configurators
 * @since 0.4 
 */
class LoggerConfiguratorXml implements LoggerConfigurator {
	const APPENDER_STATE = 1000;
	const LAYOUT_STATE = 1010;
	const ROOT_STATE = 1020;
	const LOGGER_STATE = 1030;
	const FILTER_STATE = 1040;
	
	const DEFAULT_FILENAME = './log4php.xml';
	
	/**
	 * @var string the default configuration document
	 */
	const 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
	 */
	const XMLNS = 'HTTP://LOGGING.APACHE.ORG/LOG4PHP/'; 

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

    /**
     * @var Logger parsed Logger  
     */
    private $logger;
    
    /**
     * @var LoggerAppender parsed LoggerAppender 
     */
    private $appender;
    
    /**
     * @var LoggerFilter parsed LoggerFilter 
     */
    private $filter;
    
    /**
     * @var LoggerLayout parsed LoggerLayout 
     */
    private $layout;
    
    /**
     * Constructor
     */
    public function __construct() {
        $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 function configure(LoggerHierarchy $hierarchy, $url = '') {
        return $this->doConfigure($url, $hierarchy);
    }
    
    /**
     * 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
     */
    private function doConfigure($url = '', LoggerHierarchy $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
     */
    private function doConfigureByString($xmlData, LoggerHierarchy $repository)
    {
        return $this->parse($xmlData, $repository);
    }
    
    /**
     * @param LoggerHierarchy $repository
     */
    private function doConfigureDefault(LoggerHierarchy $repository)
    {
        return $this->doConfigureByString(self::DEFAULT_CONFIGURATION, $repository);
    }
    
    /**
     * @param string $xmlData
     */
    private function parse($xmlData, LoggerHierarchy $repository)
    {
        // Logger::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);
            $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
     */
    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;
        }
    }


    /**
     * @param mixed $parser
     * @param string $tag
     */
    private function tagClose($parser, $tag)
    {
        switch ($tag) {
        
            case 'CONFIGURATION' : 
            case self::XMLNS.':CONFIGURATION':
                break;
                
            case 'APPENDER' :
            case self::XMLNS.':APPENDER':
                if ($this->appender !== null) {
                    if ($this->appender->requiresLayout() and $this->appender->getLayout() === null) {
                        $appenderName = $this->appender->getName();
                        $this->appender->setLayout(LoggerReflectionUtils::createObject('LoggerLayoutSimple'));
                    }                    
                    $this->appender->activateOptions();
                }        
                array_pop($this->state);        
                break;
                
            case 'FILTER' :
            case self::XMLNS.':FILTER':
                if ($this->filter !== null) {
                    $this->filter->activateOptions();
                    $this->appender->addFilter($this->filter);
                    $this->filter = null;
                }
                array_pop($this->state);        
                break;
                
            case 'LAYOUT':
            case self::XMLNS.':LAYOUT':
                if ($this->appender !== null and $this->layout !== null and $this->appender->requiresLayout()) {
                    $this->layout->activateOptions();
                    $this->appender->setLayout($this->layout);
                    $this->layout = null;
                }
                array_pop($this->state);
                break;
            
            case 'LOGGER':
            case self::XMLNS.':LOGGER':
                array_pop($this->state);
                break;
            
            case 'ROOT':
            case self::XMLNS.':ROOT':
                array_pop($this->state);
                break;
        }
    }
    
    private function subst($value)
    {
        return LoggerOptionConverter::substVars($value);
    }

}