library/log4php/LoggerAppenderPool.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     4  * contributor license agreements. See the NOTICE file distributed with
       
     5  * this work for additional information regarding copyright ownership.
       
     6  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     7  * (the "License"); you may not use this file except in compliance with
       
     8  * the License. You may obtain a copy of the License at
       
     9  *
       
    10  *       http://www.apache.org/licenses/LICENSE-2.0
       
    11  *
       
    12  * Unless required by applicable law or agreed to in writing, software
       
    13  * distributed under the License is distributed on an "AS IS" BASIS,
       
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    15  * See the License for the specific language governing permissions and
       
    16  * limitations under the License.
       
    17  *
       
    18  * @package log4php
       
    19  */
       
    20 
       
    21 /**
       
    22  * Pool implmentation for LoggerAppender instances.
       
    23  *
       
    24  * The pool is used when configuring log4php. First all appender instances
       
    25  * are created in the pool. Afterward, they are linked to loggers, each
       
    26  * appender can be linked to multiple loggers. This makes sure duplicate
       
    27  * appenders are not created.
       
    28  *
       
    29  * @version $Revision: 1350602 $
       
    30  * @package log4php
       
    31  */
       
    32 class LoggerAppenderPool {
       
    33 
       
    34     /** Holds appenders indexed by their name */
       
    35     public static $appenders = array();
       
    36 
       
    37     /**
       
    38      * Adds an appender to the pool.
       
    39      * The appender must be named for this operation.
       
    40      * @param LoggerAppender $appender
       
    41      */
       
    42     public static function add(LoggerAppender $appender) {
       
    43         $name = $appender->getName();
       
    44 
       
    45         if (empty($name)) {
       
    46             trigger_error('log4php: Cannot add unnamed appender to pool.', E_USER_WARNING);
       
    47             return;
       
    48         }
       
    49 
       
    50         if (isset(self::$appenders[$name])) {
       
    51             trigger_error("log4php: Appender [$name] already exists in pool. Overwriting existing appender.", E_USER_WARNING);
       
    52         }
       
    53 
       
    54         self::$appenders[$name] = $appender;
       
    55     }
       
    56 
       
    57     /**
       
    58      * Retrieves an appender from the pool by name.
       
    59      * @param string $name Name of the appender to retrieve.
       
    60      * @return LoggerAppender The named appender or NULL if no such appender
       
    61      *  exists in the pool.
       
    62      */
       
    63     public static function get($name) {
       
    64         return isset(self::$appenders[$name]) ? self::$appenders[$name] : null;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Removes an appender from the pool by name.
       
    69      * @param string $name Name of the appender to remove.
       
    70      */
       
    71     public static function delete($name) {
       
    72         unset(self::$appenders[$name]);
       
    73     }
       
    74 
       
    75     /**
       
    76      * Returns all appenders from the pool.
       
    77      * @return array Array of LoggerAppender objects.
       
    78      */
       
    79     public static function getAppenders() {
       
    80         return self::$appenders;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Checks whether an appender exists in the pool.
       
    85      * @param string $name Name of the appender to look for.
       
    86      * @return boolean TRUE if the appender with the given name exists.
       
    87      */
       
    88     public static function exists($name) {
       
    89         return isset(self::$appenders[$name]);
       
    90     }
       
    91 
       
    92     /**
       
    93      * Clears all appenders from the pool.
       
    94      */
       
    95     public static function clear() {
       
    96         self::$appenders = array();
       
    97     }
       
    98 }