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

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
82.35% 14 / 17 CRAP
86.67% 52 / 60
LoggerAppenderSyslog
0.00% 0 / 1
82.35% 14 / 17 27.60
86.67% 52 / 60
 setIdent($ident)
100.00% 1 / 1 1
100.00% 2 / 2
 setPriority($priority)
100.00% 1 / 1 1
100.00% 2 / 2
 setFacility($facility)
100.00% 1 / 1 1
100.00% 2 / 2
 setOverridePriority($overridePriority)
100.00% 1 / 1 1
100.00% 2 / 2
 setOption($option)
100.00% 1 / 1 1
100.00% 2 / 2
 getIdent()
100.00% 1 / 1 1
100.00% 1 / 1
 getPriority()
100.00% 1 / 1 1
100.00% 1 / 1
 getFacility()
100.00% 1 / 1 1
100.00% 1 / 1
 getOverridePriority()
100.00% 1 / 1 1
100.00% 1 / 1
 getOption()
100.00% 1 / 1 1
100.00% 1 / 1
 activateOptions()
100.00% 1 / 1 1
100.00% 5 / 5
 close()
0.00% 0 / 1 6
0.00% 0 / 5
 append(LoggerLoggingEvent $event)
100.00% 1 / 1 1
100.00% 6 / 6
 getSyslogPriority(LoggerLevel $level)
100.00% 1 / 1 2
100.00% 3 / 3
 parseOption()
100.00% 1 / 1 4
100.00% 12 / 12
 parseFacility()
0.00% 0 / 1 3.21
71.43% 5 / 7
 parsePriority()
0.00% 0 / 1 3.03
85.71% 6 / 7


       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                 :  * Log events to a system log using the {@link PHP_MANUAL#syslog} function.                                                                  
      23                 :  *                                                                                                                                           
      24                 :  * This appenders requires a layout.                                                                                                         
      25                 :  *                                                                                                                                           
      26                 :  * Configurable parameters:                                                                                                                  
      27                 :  *                                                                                                                                           
      28                 :  * - ident            - The ident of the syslog message.                                                                                     
      29                 :  * - priority         - The priority for the syslog message (used when overriding priority).                                                 
      30                 :  * - facility         - The facility for the syslog message                                                                                  
      31                 :  * - overridePriority - If set to true, the message priority will always use                                                                 
      32                 :  *                      the value defined in {@link $priority}, otherwise the                                                                
      33                 :  *                      priority will be determined by the message's log level.                                                              
      34                 :  * - option           - The option value for the syslog message.                                                                             
      35                 :  *                                                                                                                                           
      36                 :  * Recognised syslog options are:                                                                                                            
      37                 :  *     - CONS      - if there is an error while sending data to the system logger, write directly to the system console                      
      38                 :  *     - NDELAY - open the connection to the logger immediately                                                                              
      39                 :  *     - ODELAY - delay opening the connection until the first message is logged (default)                                                   
      40                 :  *     - PERROR - print log message also to standard error                                                                                   
      41                 :  *     - PID    - include PID with each message                                                                                              
      42                 :  *                                                                                                                                           
      43                 :  * Multiple options can be set by delimiting them with a pipe character,                                                                     
      44                 :  * e.g.: "CONS|PID|PERROR".                                                                                                                  
      45                 :  *                                                                                                                                           
      46                 :  * Recognised syslog priorities are:                                                                                                         
      47                 :  *     - EMERG                                                                                                                               
      48                 :  *     - ALERT                                                                                                                               
      49                 :  *     - CRIT                                                                                                                                
      50                 :  *     - ERR                                                                                                                                 
      51                 :  *     - WARNING                                                                                                                             
      52                 :  *     - NOTICE                                                                                                                              
      53                 :  *     - INFO                                                                                                                                
      54                 :  *     - DEBUG                                                                                                                               
      55                 :  *                                                                                                                                           
      56                 :  * Levels are mapped as follows:                                                                                                             
      57                 :  * - <b>FATAL</b> to LOG_ALERT                                                                                                               
      58                 :  * - <b>ERROR</b> to LOG_ERR                                                                                                                 
      59                 :  * - <b>WARN</b> to LOG_WARNING                                                                                                              
      60                 :  * - <b>INFO</b> to LOG_INFO                                                                                                                 
      61                 :  * - <b>DEBUG</b> to LOG_DEBUG                                                                                                               
      62                 :  * - <b>TRACE</b> to LOG_DEBUG                                                                                                               
      63                 :  *                                                                                                                                           
      64                 :  * An example:                                                                                                                               
      65                 :  *                                                                                                                                           
      66                 :  * {@example ../../examples/php/appender_syslog.php 19}                                                                                      
      67                 :  *                                                                                                                                           
      68                 :  * {@example ../../examples/resources/appender_syslog.properties 18}                                                                         
      69                 :  *                                                                                                                                           
      70                 :  * @version $Revision: 1230527 $                                                                                                             
      71                 :  * @package log4php                                                                                                                          
      72                 :  * @subpackage appenders                                                                                                                     
      73                 :  */                                                                                                                                          
      74                 : class LoggerAppenderSyslog extends LoggerAppender {                                                                                          
      75                 :                                                                                                                                              
      76                 :     /**                                                                                                                                      
      77                 :      * The ident string is added to each message. Typically the name of your application.                                                    
      78                 :      *                                                                                                                                       
      79                 :      * @var string                                                                                                                           
      80                 :      */                                                                                                                                      
      81                 :     protected $ident = "Apache log4php";                                                                                                     
      82                 :                                                                                                                                              
      83                 :     /**                                                                                                                                      
      84                 :      * The syslog priority to use when overriding priority. This setting is                                                                  
      85                 :      * required if {@link overridePriority} is set to true.                                                                                  
      86                 :      *                                                                                                                                       
      87                 :      * @var string                                                                                                                           
      88                 :      */                                                                                                                                      
      89                 :     protected $priority;                                                                                                                     
      90                 :                                                                                                                                              
      91                 :     /**                                                                                                                                      
      92                 :      * The option used when opening the syslog connection.                                                                                   
      93                 :      *                                                                                                                                       
      94                 :      * @var string                                                                                                                           
      95                 :      */                                                                                                                                      
      96                 :     protected $option = 'PID|CONS';                                                                                                          
      97                 :                                                                                                                                              
      98                 :     /**                                                                                                                                      
      99                 :      * The facility value indicates the source of the message.                                                                               
     100                 :      *                                                                                                                                       
     101                 :      * @var string                                                                                                                           
     102                 :      */                                                                                                                                      
     103                 :     protected $facility = 'USER';                                                                                                            
     104                 :                                                                                                                                              
     105                 :     /**                                                                                                                                      
     106                 :      * If set to true, the message priority will always use the value defined                                                                
     107                 :      * in {@link $priority}, otherwise the priority will be determined by the                                                                
     108                 :      * message's log level.                                                                                                                  
     109                 :      *                                                                                                                                       
     110                 :      * @var string                                                                                                                           
     111                 :      */                                                                                                                                      
     112                 :     protected $overridePriority = false;                                                                                                     
     113                 :                                                                                                                                              
     114                 :     /**                                                                                                                                      
     115                 :      * Holds the int value of the {@link $priority}.                                                                                         
     116                 :      * @var int                                                                                                                              
     117                 :      */                                                                                                                                      
     118                 :     private $intPriority;                                                                                                                    
     119                 :                                                                                                                                              
     120                 :     /**                                                                                                                                      
     121                 :      * Holds the int value of the {@link $facility}.                                                                                         
     122                 :      * @var int                                                                                                                              
     123                 :      */                                                                                                                                      
     124                 :     private $intFacility;                                                                                                                    
     125                 :                                                                                                                                              
     126                 :     /**                                                                                                                                      
     127                 :      * Holds the int value of the {@link $option}.                                                                                           
     128                 :      * @var int                                                                                                                              
     129                 :      */                                                                                                                                      
     130                 :     private $intOption;                                                                                                                      
     131                 :                                                                                                                                              
     132                 :     /**                                                                                                                                      
     133                 :      * Sets the {@link $ident}.                                                                                                              
     134                 :      *                                                                                                                                       
     135                 :      * @param string $ident                                                                                                                  
     136                 :      */                                                                                                                                      
     137                 :     public function setIdent($ident) {                                                                                                       
     138               1 :         $this->ident = $ident;                                                                                                               
     139               1 :     }                                                                                                                                        
     140                 :                                                                                                                                              
     141                 :     /**                                                                                                                                      
     142                 :      * Sets the {@link $priority}.                                                                                                           
     143                 :      *                                                                                                                                       
     144                 :      * @param string $priority                                                                                                               
     145                 :      */                                                                                                                                      
     146                 :     public function setPriority($priority) {                                                                                                 
     147               4 :         $this->priority = $priority;                                                                                                         
     148               4 :     }                                                                                                                                        
     149                 :                                                                                                                                              
     150                 :     /**                                                                                                                                      
     151                 :      * Sets the {@link $facility}.                                                                                                           
     152                 :      *                                                                                                                                       
     153                 :      * @param string $facility                                                                                                               
     154                 :      */                                                                                                                                      
     155                 :     public function setFacility($facility) {                                                                                                 
     156               3 :         $this->facility = $facility;                                                                                                         
     157               3 :     }                                                                                                                                        
     158                 :                                                                                                                                              
     159                 :     /**                                                                                                                                      
     160                 :      * Sets the {@link $overridePriority}.                                                                                                   
     161                 :      *                                                                                                                                       
     162                 :      * @param string $overridePriority                                                                                                       
     163                 :      */                                                                                                                                      
     164                 :     public function setOverridePriority($overridePriority) {                                                                                 
     165               2 :         $this->overridePriority = $overridePriority;                                                                                         
     166               2 :     }                                                                                                                                        
     167                 :                                                                                                                                              
     168                 :     /**                                                                                                                                      
     169                 :     * Sets the {@link $option}.                                                                                                              
     170                 :     *                                                                                                                                        
     171                 :     * @param string $option                                                                                                                  
     172                 :     */                                                                                                                                       
     173                 :     public function setOption($option) {                                                                                                     
     174               3 :         $this->option = $option;                                                                                                             
     175               3 :     }                                                                                                                                        
     176                 :                                                                                                                                              
     177                 :     /**                                                                                                                                      
     178                 :     * Returns the {@link $ident}.                                                                                                            
     179                 :     *                                                                                                                                        
     180                 :     * @return string $ident                                                                                                                  
     181                 :     */                                                                                                                                       
     182                 :     public function getIdent() {                                                                                                             
     183               1 :         return $this->ident;                                                                                                                 
     184                 :     }                                                                                                                                        
     185                 :                                                                                                                                              
     186                 :     /**                                                                                                                                      
     187                 :      * Returns the {@link $priority}.                                                                                                        
     188                 :      *                                                                                                                                       
     189                 :      * @return string                                                                                                                        
     190                 :      */                                                                                                                                      
     191                 :     public function getPriority() {                                                                                                          
     192               1 :         return $this->priority;                                                                                                              
     193                 :     }                                                                                                                                        
     194                 :                                                                                                                                              
     195                 :     /**                                                                                                                                      
     196                 :      * Returns the {@link $facility}.                                                                                                        
     197                 :      *                                                                                                                                       
     198                 :      * @return string                                                                                                                        
     199                 :      */                                                                                                                                      
     200                 :     public function getFacility() {                                                                                                          
     201               1 :         return $this->facility;                                                                                                              
     202                 :     }                                                                                                                                        
     203                 :                                                                                                                                              
     204                 :     /**                                                                                                                                      
     205                 :      * Returns the {@link $overridePriority}.                                                                                                
     206                 :      *                                                                                                                                       
     207                 :      * @return string                                                                                                                        
     208                 :      */                                                                                                                                      
     209                 :     public function getOverridePriority() {                                                                                                  
     210               1 :         return $this->overridePriority;                                                                                                      
     211                 :     }                                                                                                                                        
     212                 :                                                                                                                                              
     213                 :     /**                                                                                                                                      
     214                 :      * Returns the {@link $option}.                                                                                                          
     215                 :      *                                                                                                                                       
     216                 :      * @return string                                                                                                                        
     217                 :      */                                                                                                                                      
     218                 :     public function getOption() {                                                                                                            
     219               1 :         return $this->option;                                                                                                                
     220                 :     }                                                                                                                                        
     221                 :                                                                                                                                              
     222                 :                                                                                                                                              
     223                 :     public function activateOptions() {                                                                                                      
     224               8 :         $this->intPriority = $this->parsePriority();                                                                                         
     225               7 :         $this->intOption   = $this->parseOption();                                                                                           
     226               6 :         $this->intFacility = $this->parseFacility();                                                                                         
     227                 :                                                                                                                                              
     228               5 :         $this->closed = false;                                                                                                               
     229               5 :     }                                                                                                                                        
     230                 :                                                                                                                                              
     231                 :     public function close() {                                                                                                                
     232               0 :         if($this->closed != true) {                                                                                                          
     233               0 :             closelog();                                                                                                                      
     234               0 :             $this->closed = true;                                                                                                            
     235               0 :         }                                                                                                                                    
     236               0 :     }                                                                                                                                        
     237                 :                                                                                                                                              
     238                 :     /**                                                                                                                                      
     239                 :      * Appends the event to syslog.                                                                                                          
     240                 :      *                                                                                                                                       
     241                 :      * Log is opened and closed each time because if it is not closed, it                                                                    
     242                 :      * can cause the Apache httpd server to log to whatever ident/facility                                                                   
     243                 :      * was used in openlog().                                                                                                                
     244                 :      *                                                                                                                                       
     245                 :      * @see http://www.php.net/manual/en/function.syslog.php#97843                                                                           
     246                 :      */                                                                                                                                      
     247                 :     public function append(LoggerLoggingEvent $event) {                                                                                      
     248               1 :         $priority = $this->getSyslogPriority($event->getLevel());                                                                            
     249               1 :         $message = $this->layout->format($event);                                                                                            
     250                 :                                                                                                                                              
     251               1 :         openlog($this->ident, $this->intOption, $this->intFacility);                                                                         
     252               1 :         syslog($priority, $message);                                                                                                         
     253               1 :         closelog();                                                                                                                          
     254               1 :     }                                                                                                                                        
     255                 :                                                                                                                                              
     256                 :     /** Determines which syslog priority to use based on the given level. */                                                                 
     257                 :     private function getSyslogPriority(LoggerLevel $level) {                                                                                 
     258               2 :         if($this->overridePriority) {                                                                                                        
     259               1 :             return $this->intPriority;                                                                                                       
     260                 :         }                                                                                                                                    
     261               1 :         return $level->getSyslogEquivalent();                                                                                                
     262                 :     }                                                                                                                                        
     263                 :                                                                                                                                              
     264                 :     /** Parses a syslog option string and returns the correspodning int value. */                                                            
     265                 :     private function parseOption() {                                                                                                         
     266               7 :         $value = 0;                                                                                                                          
     267               7 :         $options = explode('|', $this->option);                                                                                              
     268                 :                                                                                                                                              
     269               7 :         foreach($options as $option) {                                                                                                       
     270               7 :             if (!empty($option)) {                                                                                                           
     271               7 :                 $constant = "LOG_" . trim($option);                                                                                          
     272               7 :                 if (defined($constant)) {                                                                                                    
     273               7 :                     $value |= constant($constant);                                                                                           
     274               7 :                 } else {                                                                                                                     
     275               1 :                     trigger_error("log4php: Invalid syslog option provided: $option. Whole option string: {$this->option}.", E_USER_WARNING);
     276                 :                 }                                                                                                                            
     277               7 :             }                                                                                                                                
     278               7 :         }                                                                                                                                    
     279               6 :         return $value;                                                                                                                       
     280                 :     }                                                                                                                                        
     281                 :                                                                                                                                              
     282                 :     /** Parses the facility string and returns the corresponding int value. */                                                               
     283                 :     private function parseFacility() {                                                                                                       
     284               6 :         if (!empty($this->facility)) {                                                                                                       
     285               6 :             $constant = "LOG_" . trim($this->facility);                                                                                      
     286               6 :             if (defined($constant)) {                                                                                                        
     287               5 :                 return constant($constant);                                                                                                  
     288                 :             } else {                                                                                                                         
     289               1 :                 trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);                              
     290                 :             }                                                                                                                                
     291               0 :         }                                                                                                                                    
     292               0 :     }                                                                                                                                        
     293                 :                                                                                                                                              
     294                 :     /** Parses the priority string and returns the corresponding int value. */                                                               
     295                 :     private function parsePriority() {                                                                                                       
     296               8 :         if (!empty($this->priority)) {                                                                                                       
     297               3 :             $constant = "LOG_" . trim($this->priority);                                                                                      
     298               3 :             if (defined($constant)) {                                                                                                        
     299               2 :                 return constant($constant);                                                                                                  
     300                 :             } else {                                                                                                                         
     301               1 :                 trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);                              
     302                 :             }                                                                                                                                
     303               0 :         }                                                                                                                                    
     304               6 :     }                                                                                                                                        
     305                 : }                                                                                                                                            

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.