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

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
83.33% 10 / 12 CRAP
94.44% 51 / 54
LoggerHierarchy
0.00% 0 / 1
83.33% 10 / 12 23.09
94.44% 51 / 54
 __construct(LoggerRoot $root)
100.00% 1 / 1 1
100.00% 4 / 4
 clear()
100.00% 1 / 1 1
100.00% 2 / 2
 exists($name)
100.00% 1 / 1 1
100.00% 1 / 1
 getCurrentLoggers()
100.00% 1 / 1 1
100.00% 1 / 1
 getLogger($name)
100.00% 1 / 1 8
100.00% 20 / 20
 getRendererMap()
100.00% 1 / 1 1
100.00% 1 / 1
 getRootLogger()
0.00% 0 / 1 4.12
50.00% 2 / 4
 getThreshold()
100.00% 1 / 1 1
100.00% 1 / 1
 isDisabled(LoggerLevel $level)
0.00% 0 / 1 2
0.00% 0 / 1
 resetConfiguration()
100.00% 1 / 1 2
100.00% 12 / 12
 setThreshold(LoggerLevel $threshold)
100.00% 1 / 1 1
100.00% 2 / 2
 shutdown()
100.00% 1 / 1 2
100.00% 5 / 5


       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                 :  * This class is specialized in retrieving loggers by name and also maintaining      
      23                 :  * the logger hierarchy. The logger hierarchy is dealing with the several Log-Levels 
      24                 :  * Logger can have. From log4j website:                                              
      25                 :  *                                                                                   
      26                 :  * "A logger is said to be an ancestor of another logger if its name followed        
      27                 :  * by a dot is a prefix of the descendant logger name. A logger is said to be        
      28                 :  * a parent of a child logger if there are no ancestors between itself and the       
      29                 :  * descendant logger."                                                               
      30                 :  *                                                                                   
      31                 :  * Child Loggers do inherit their Log-Levels from their Ancestors. They can          
      32                 :  * increase their Log-Level compared to their Ancestors, but they cannot decrease it.
      33                 :  *                                                                                   
      34                 :  * <p>The casual user does not have to deal with this class directly.</p>            
      35                 :  *                                                                                   
      36                 :  * <p>The structure of the logger hierarchy is maintained by the                     
      37                 :  * getLogger method. The hierarchy is such that children link                        
      38                 :  * to their parent but parents do not have any pointers to their                     
      39                 :  * children. Moreover, loggers can be instantiated in any order, in                  
      40                 :  * particular descendant before ancestor.</p>                                        
      41                 :  *                                                                                   
      42                 :  * <p>In case a descendant is created before a particular ancestor,                  
      43                 :  * then it creates a provision node for the ancestor and adds itself                 
      44                 :  * to the provision node. Other descendants of the same ancestor add                 
      45                 :  * themselves to the previously created provision node.</p>                          
      46                 :  *                                                                                   
      47                 :  * @version $Revision: 1163124 $                                                     
      48                 :  * @package log4php                                                                  
      49                 :  */                                                                                  
      50                 : class LoggerHierarchy {                                                              
      51                 :                                                                                      
      52                 :     /** Array holding all Logger instances. */                                       
      53                 :     protected $loggers = array();                                                    
      54                 :                                                                                      
      55                 :     /**                                                                              
      56                 :      * The root logger.                                                              
      57                 :      * @var RootLogger                                                               
      58                 :      */                                                                              
      59                 :     protected $root = null;                                                          
      60                 :                                                                                      
      61                 :     /**                                                                              
      62                 :      * The logger renderer map.                                                      
      63                 :      * @var LoggerRendererMap                                                        
      64                 :      */                                                                              
      65                 :     protected $rendererMap;                                                          
      66                 :                                                                                      
      67                 :     /**                                                                              
      68                 :      * Main level threshold. Events with lower level will not be logged by any       
      69                 :      * logger, regardless of it's configuration.                                     
      70                 :      * @var LoggerLevel                                                              
      71                 :      */                                                                              
      72                 :     protected $threshold;                                                            
      73                 :                                                                                      
      74                 :     /**                                                                              
      75                 :      * Creates a new logger hierarchy.                                               
      76                 :      * @param LoggerRoot $root The root logger.                                      
      77                 :      */                                                                              
      78                 :     public function __construct(LoggerRoot $root) {                                  
      79              10 :         $this->root = $root;                                                         
      80              10 :         $this->setThreshold(LoggerLevel::getLevelAll());                             
      81              10 :         $this->rendererMap = new LoggerRendererMap();                                
      82              10 :     }                                                                                
      83                 :                                                                                      
      84                 :     /**                                                                              
      85                 :      * Clears all loggers.                                                           
      86                 :      */                                                                              
      87                 :     public function clear() {                                                        
      88              64 :         $this->loggers = array();                                                    
      89              64 :     }                                                                                
      90                 :                                                                                      
      91                 :     /**                                                                              
      92                 :      * Check if the named logger exists in the hierarchy.                            
      93                 :      * @param string $name                                                           
      94                 :      * @return boolean                                                               
      95                 :      */                                                                              
      96                 :     public function exists($name) {                                                  
      97               3 :         return isset($this->loggers[$name]);                                         
      98                 :     }                                                                                
      99                 :                                                                                      
     100                 :     /**                                                                              
     101                 :      * Returns all the currently defined loggers in this hierarchy as an array.      
     102                 :      * @return array                                                                 
     103                 :      */                                                                              
     104                 :     public function getCurrentLoggers() {                                            
     105               4 :         return array_values($this->loggers);                                         
     106                 :     }                                                                                
     107                 :                                                                                      
     108                 :     /**                                                                              
     109                 :      * Returns a named logger instance logger. If it doesn't exist, one is created.  
     110                 :      *                                                                               
     111                 :      * @param string $name Logger name                                               
     112                 :      * @return Logger Logger instance.                                               
     113                 :      */                                                                              
     114                 :     public function getLogger($name) {                                               
     115              23 :         if(!isset($this->loggers[$name])) {                                          
     116              21 :             $logger = new Logger($name);                                             
     117                 :                                                                                      
     118              21 :             $nodes = explode('.', $name);                                            
     119              21 :             $firstNode = array_shift($nodes);                                        
     120                 :                                                                                      
     121                 :             // if name is not a first node but another first node is their           
     122              21 :             if($firstNode != $name and isset($this->loggers[$firstNode])) {          
     123               2 :                 $logger->setParent($this->loggers[$firstNode]);                      
     124               2 :             } else {                                                                 
     125                 :                 // if there is no father, set root logger as father                  
     126              21 :                 $logger->setParent($this->root);                                     
     127                 :             }                                                                        
     128                 :                                                                                      
     129                 :             // if there are more nodes than one                                      
     130              21 :             if(count($nodes) > 0) {                                                  
     131                 :                 // find parent node                                                  
     132               2 :                 foreach($nodes as $node) {                                           
     133               2 :                     $parentNode = "$firstNode.$node";                                
     134               2 :                     if(isset($this->loggers[$parentNode]) and $parentNode != $name) {
     135               1 :                         $logger->setParent($this->loggers[$parentNode]);             
     136               1 :                     }                                                                
     137               2 :                     $firstNode .= ".$node";                                          
     138               2 :                 }                                                                    
     139               2 :             }                                                                        
     140                 :                                                                                      
     141              21 :             $this->loggers[$name] = $logger;                                         
     142              21 :         }                                                                            
     143                 :                                                                                      
     144              23 :         return $this->loggers[$name];                                                
     145                 :     }                                                                                
     146                 :                                                                                      
     147                 :     /**                                                                              
     148                 :      * Returns the logger renderer map.                                              
     149                 :      * @return LoggerRendererMap                                                     
     150                 :      */                                                                              
     151                 :     public function getRendererMap() {                                               
     152               9 :         return $this->rendererMap;                                                   
     153                 :     }                                                                                
     154                 :                                                                                      
     155                 :     /**                                                                              
     156                 :      * Returns the root logger.                                                      
     157                 :      * @return LoggerRoot                                                            
     158                 :      */                                                                              
     159                 :     public function getRootLogger() {                                                
     160              70 :         if(!isset($this->root) or $this->root == null) {                             
     161               0 :             $this->root = new LoggerRoot();                                          
     162               0 :         }                                                                            
     163              70 :         return $this->root;                                                          
     164                 :     }                                                                                
     165                 :                                                                                      
     166                 :     /**                                                                              
     167                 :      * Returns the main threshold level.                                             
     168                 :      * @return LoggerLevel                                                           
     169                 :      */                                                                              
     170                 :     public function getThreshold() {                                                 
     171               2 :         return $this->threshold;                                                     
     172                 :     }                                                                                
     173                 :                                                                                      
     174                 :     /**                                                                              
     175                 :      * Returns true if the hierarchy is disabled for given log level and false       
     176                 :      * otherwise.                                                                    
     177                 :      * @return boolean                                                               
     178                 :      */                                                                              
     179                 :     public function isDisabled(LoggerLevel $level) {                                 
     180               0 :         return ($this->threshold->toInt() > $level->toInt());                        
     181                 :     }                                                                                
     182                 :                                                                                      
     183                 :     /**                                                                              
     184                 :      * Reset all values contained in this hierarchy instance to their                
     185                 :      * default.                                                                      
     186                 :      *                                                                               
     187                 :      * This removes all appenders from all loggers, sets                             
     188                 :      * the level of all non-root loggers to <i>null</i>,                             
     189                 :      * sets their additivity flag to <i>true</i> and sets the level                  
     190                 :      * of the root logger to {@link LOGGER_LEVEL_DEBUG}.                             
     191                 :      *                                                                               
     192                 :      * <p>Existing loggers are not removed. They are just reset.                     
     193                 :      *                                                                               
     194                 :      * <p>This method should be used sparingly and with care as it will              
     195                 :      * block all logging until it is completed.</p>                                  
     196                 :      */                                                                              
     197                 :     public function resetConfiguration() {                                           
     198              63 :         $root = $this->getRootLogger();                                              
     199                 :                                                                                      
     200              63 :         $root->setLevel(LoggerLevel::getLevelDebug());                               
     201              63 :         $this->setThreshold(LoggerLevel::getLevelAll());                             
     202              63 :         $this->shutDown();                                                           
     203                 :                                                                                      
     204              63 :         foreach($this->loggers as $logger) {                                         
     205               8 :             $logger->setLevel(null);                                                 
     206               8 :             $logger->setAdditivity(true);                                            
     207               8 :             $logger->removeAllAppenders();                                           
     208              63 :         }                                                                            
     209                 :                                                                                      
     210              63 :         $this->rendererMap->clear();                                                 
     211              63 :         LoggerAppenderPool::clear();                                                 
     212              63 :     }                                                                                
     213                 :                                                                                      
     214                 :     /**                                                                              
     215                 :      * Sets the main threshold level.                                                
     216                 :      * @param LoggerLevel $l                                                         
     217                 :      */                                                                              
     218                 :     public function setThreshold(LoggerLevel $threshold) {                           
     219              71 :         $this->threshold = $threshold;                                               
     220              71 :     }                                                                                
     221                 :                                                                                      
     222                 :     /**                                                                              
     223                 :      * Shutting down a hierarchy will <i>safely</i> close and remove                 
     224                 :      * all appenders in all loggers including the root logger.                       
     225                 :      *                                                                               
     226                 :      * The shutdown method is careful to close nested                                
     227                 :      * appenders before closing regular appenders. This is allows                    
     228                 :      * configurations where a regular appender is attached to a logger               
     229                 :      * and again to a nested appender.                                               
     230                 :      *                                                                               
     231                 :      * @todo Check if the last paragraph is correct.                                 
     232                 :      */                                                                              
     233                 :     public function shutdown() {                                                     
     234              66 :         $this->root->removeAllAppenders();                                           
     235                 :                                                                                      
     236              66 :         foreach($this->loggers as $logger) {                                         
     237              11 :             $logger->removeAllAppenders();                                           
     238              66 :         }                                                                            
     239              66 :     }                                                                                
     240                 : }                                                                                    

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.