library/log4php/LoggerMDC.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     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  * The LoggerMDC class provides _mapped diagnostic contexts_.
       
    23  *
       
    24  * A Mapped Diagnostic Context, or MDC in short, is an instrument for
       
    25  * distinguishing interleaved log output from different sources. Log output
       
    26  * is typically interleaved when a server handles multiple clients
       
    27  * near-simultaneously.
       
    28  *
       
    29  * This class is similar to the {@link LoggerNDC} class except that
       
    30  * it is based on a map instead of a stack.
       
    31  *
       
    32  * @version $Revision: 1343630 $
       
    33  * @since 0.3
       
    34  * @package log4php
       
    35  */
       
    36 class LoggerMDC {
       
    37 
       
    38     /** Holds the context map. */
       
    39     private static $map = array();
       
    40 
       
    41     /**
       
    42      * Stores a context value as identified with the key parameter into the
       
    43      * context map.
       
    44      *
       
    45      * @param string $key the key
       
    46      * @param string $value the value
       
    47      */
       
    48     public static function put($key, $value) {
       
    49         self::$map[$key] = $value;
       
    50     }
       
    51 
       
    52     /**
       
    53      * Returns the context value identified by the key parameter.
       
    54      *
       
    55      * @param string $key The key.
       
    56      * @return string The context or an empty string if no context found
       
    57      *    for given key.
       
    58      */
       
    59     public static function get($key) {
       
    60         return isset(self::$map[$key]) ? self::$map[$key] : '';
       
    61     }
       
    62 
       
    63     /**
       
    64      * Returns the contex map as an array.
       
    65      * @return array The MDC context map.
       
    66      */
       
    67     public static function getMap() {
       
    68         return self::$map;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Removes the the context identified by the key parameter.
       
    73      *
       
    74      * Only affects user mappings, not $_ENV or $_SERVER.
       
    75      *
       
    76      * @param string $key The key to be removed.
       
    77      */
       
    78     public static function remove($key) {
       
    79         unset(self::$map[$key]);
       
    80     }
       
    81 
       
    82     /**
       
    83      * Clears the mapped diagnostic context.
       
    84      */
       
    85     public static function clear() {
       
    86         self::$map = array();
       
    87     }
       
    88 }