LoggerMDC.php
Current file: /home/ihabunek/apache/log4php/src/main/php/LoggerMDC.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00% 1 / 1
100.00% 5 / 5 CRAP
100.00% 17 / 17
LoggerMDC
100.00% 1 / 1
100.00% 5 / 5 11
100.00% 17 / 17
 put($key, $value)
100.00% 1 / 1 1
100.00% 2 / 2
 get($key)
100.00% 1 / 1 7
100.00% 10 / 10
 getMap()
100.00% 1 / 1 1
100.00% 1 / 1
 remove($key)
100.00% 1 / 1 1
100.00% 2 / 2
 clear()
100.00% 1 / 1 1
100.00% 2 / 2


       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 <i>mapped diagnostic contexts</i>.                   
      23                 :  *                                                                                   
      24                 :  * A <i>Mapped Diagnostic Context</i>, or                                            
      25                 :  * MDC in short, is an instrument for distinguishing interleaved log                 
      26                 :  * output from different sources. Log output is typically interleaved                
      27                 :  * when a server handles multiple clients 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                 :  * Example:                                                                          
      33                 :  *                                                                                   
      34                 :  * {@example ../../examples/php/mdc.php 19}<br>                                      
      35                 :  *                                                                                   
      36                 :  * With the properties file:                                                         
      37                 :  *                                                                                   
      38                 :  * {@example ../../examples/resources/mdc.properties 18}<br>                         
      39                 :  *                                                                                   
      40                 :  * Will result in the following (notice the username "knut" in the output):          
      41                 :  *                                                                                   
      42                 :  * <pre>                                                                             
      43                 :  * 2009-09-13 18:48:28 DEBUG root knut: Testing MDC in src/examples/php/mdc.php at 23
      44                 :  * </pre>                                                                            
      45                 :  *                                                                                   
      46                 :  * @version $Revision: 1212773 $                                                     
      47                 :  * @since 0.3                                                                        
      48                 :  * @package log4php                                                                  
      49                 :  */                                                                                  
      50                 : class LoggerMDC {                                                                    
      51                 :                                                                                      
      52                 :     /** Holds the context map. */                                                    
      53                 :     private static $map = array();                                                   
      54                 :                                                                                      
      55                 :     /**                                                                              
      56                 :      * Stores a context value as identified with the key parameter into the          
      57                 :      * context map.                                                                  
      58                 :      *                                                                               
      59                 :      * @param string $key the key                                                    
      60                 :      * @param string $value the value                                                
      61                 :      */                                                                              
      62                 :     public static function put($key, $value) {                                       
      63               2 :         self::$map[$key] = $value;                                                   
      64               2 :     }                                                                                
      65                 :                                                                                      
      66                 :     /**                                                                              
      67                 :      * Returns the context value identified by the key parameter.                    
      68                 :      *                                                                               
      69                 :      * Special key identifiers can be used to map values in the global $_SERVER      
      70                 :      * and $_ENV vars. To access them, use 'server.' or 'env.' followed by the       
      71                 :      * desired var name as the key.                                                  
      72                 :      *                                                                               
      73                 :      * @param string $key The key.                                                   
      74                 :      * @return string The context or an empty string if no context found             
      75                 :      *     for given key.                                                            
      76                 :      */                                                                              
      77                 :     public static function get($key) {                                               
      78               3 :         if(!empty($key)) {                                                           
      79               3 :             if(strpos($key, 'server.') === 0) {                                      
      80               1 :                 $varName = substr($key, 7);                                          
      81               1 :                 return isset($_SERVER[$varName]) ? $_SERVER[$varName] : '';          
      82               2 :             } else if(strpos($key, 'env.') === 0) {                                  
      83               1 :                 $varName = substr($key, 4);                                          
      84               1 :                 $value = getenv($varName);                                           
      85               1 :                 return ($value !== false) ? $value : '';                             
      86                 :             } else {                                                                 
      87               1 :                 return isset(self::$map[$key]) ? self::$map[$key] : '';              
      88                 :             }                                                                        
      89                 :         }                                                                            
      90               1 :         return '';                                                                   
      91                 :     }                                                                                
      92                 :                                                                                      
      93                 :     /**                                                                              
      94                 :      * Returns the contex map as an array.                                           
      95                 :      * @return array The MDC context map.                                            
      96                 :      */                                                                              
      97                 :     public static function getMap() {                                                
      98               6 :         return self::$map;                                                           
      99                 :     }                                                                                
     100                 :                                                                                      
     101                 :     /**                                                                              
     102                 :      * Removes the the context identified by the key parameter.                      
     103                 :      *                                                                               
     104                 :      * Only affects user mappings, not $_ENV or $_SERVER.                            
     105                 :      *                                                                               
     106                 :      * @param string $key The key to be removed.                                     
     107                 :      */                                                                              
     108                 :     public static function remove($key) {                                            
     109               1 :         unset(self::$map[$key]);                                                     
     110               1 :     }                                                                                
     111                 :                                                                                      
     112                 :     /**                                                                              
     113                 :      * Clears the mapped diagnostic context.                                         
     114                 :      */                                                                              
     115                 :     public static function clear() {                                                 
     116               4 :         self::$map = array();                                                        
     117               4 :     }                                                                                
     118                 : }                                                                                    

Generated by PHP_CodeCoverage 1.1.1 using PHP 5.3.3-7+squeeze3 and PHPUnit 3.6.3 at Sat Feb 18 22:32:39 GMT 2012.