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

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
66.67% 2 / 3 CRAP
84.62% 22 / 26
LoggerAppenderDailyFile
0.00% 0 / 1
66.67% 2 / 3 9.29
84.62% 22 / 26
 setDatePattern($datePattern)
100.00% 1 / 1 1
100.00% 2 / 2
 getDatePattern()
100.00% 1 / 1 1
100.00% 1 / 1
 activateOptions()
0.00% 0 / 1 7.26
82.61% 19 / 23


       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                 :  * An Appender that automatically creates a new logfile each day.                                                           
      23                 :  *                                                                                                                          
      24                 :  * The file is rolled over once a day. That means, for each day a new file                                                  
      25                 :  * is created. A formatted version of the date pattern is used as to create                                                 
      26                 :  * the file name using the {@link PHP_MANUAL#sprintf} function.                                                             
      27                 :  *                                                                                                                          
      28                 :  * This appender uses a layout.                                                                                             
      29                 :  *                                                                                                                          
      30                 :  * Configurable parameters for this appender are:                                                                           
      31                 :  * - datePattern - The date format for the file name. Should be set before                                                  
      32                 :  *                 $file. Default value: "Ymd".                                                                             
      33                 :  * - file        - The path to the target log file. The filename should                                                     
      34                 :  *                 contain a '%s' which will be substituted by the date.                                                    
      35                 :  * - append      - Sets if the appender should append to the end of the                                                     
      36                 :  *                 file or overwrite content ("true" or "false"). Default                                                   
      37                 :  *                 value: true.                                                                                             
      38                 :  *                                                                                                                          
      39                 :  * An example php file:                                                                                                     
      40                 :  *                                                                                                                          
      41                 :  * {@example ../../examples/php/appender_dailyfile.php 19}                                                                  
      42                 :  *                                                                                                                          
      43                 :  * An example configuration file:                                                                                           
      44                 :  *                                                                                                                          
      45                 :  * {@example ../../examples/resources/appender_dailyfile.properties 18}                                                     
      46                 :  *                                                                                                                          
      47                 :  * The above will create a file like: daily_20090908.log                                                                    
      48                 :  *                                                                                                                          
      49                 :  * @version $Revision: 1213283 $                                                                                            
      50                 :  * @package log4php                                                                                                         
      51                 :  * @subpackage appenders                                                                                                    
      52                 :  */                                                                                                                         
      53                 : class LoggerAppenderDailyFile extends LoggerAppenderFile {                                                                  
      54                 :                                                                                                                             
      55                 :     /**                                                                                                                     
      56                 :      * Format date.                                                                                                         
      57                 :      * It follows the {@link PHP_MANUAL#date()} formatting rules and <b>should always be set before {@link $file} param</b>.
      58                 :      * @var string                                                                                                          
      59                 :      */                                                                                                                     
      60                 :     protected $datePattern = "Ymd";                                                                                         
      61                 :                                                                                                                             
      62                 :     /**                                                                                                                     
      63                 :      * Sets date format for the file name.                                                                                  
      64                 :      * @param string $datePattern a regular date() string format                                                            
      65                 :      */                                                                                                                     
      66                 :     public function setDatePattern($datePattern) {                                                                          
      67               1 :         $this->setString('datePattern', $datePattern);                                                                      
      68               1 :     }                                                                                                                       
      69                 :                                                                                                                             
      70                 :     /**                                                                                                                     
      71                 :      * @return string returns date format for the filename                                                                  
      72                 :      */                                                                                                                     
      73                 :     public function getDatePattern() {                                                                                      
      74               2 :         return $this->datePattern;                                                                                          
      75                 :     }                                                                                                                       
      76                 :                                                                                                                             
      77                 :     /**                                                                                                                     
      78                 :      * Similar to parent method, but but replaces "%s" in the file name with                                                
      79                 :      * the current date in format specified by the 'datePattern' parameter.                                                 
      80                 :      */                                                                                                                     
      81                 :     public function activateOptions() {                                                                                     
      82               2 :         $fileName = $this->getFile();                                                                                       
      83               2 :         $date = date($this->getDatePattern());                                                                              
      84               2 :         $fileName = sprintf($fileName, $date);                                                                              
      85                 :                                                                                                                             
      86               2 :         if(!is_file($fileName)) {                                                                                           
      87               2 :             $dir = dirname($fileName);                                                                                      
      88               2 :             if(!is_dir($dir)) {                                                                                             
      89               0 :                 mkdir($dir, 0777, true);                                                                                    
      90               0 :             }                                                                                                               
      91               2 :         }                                                                                                                   
      92                 :                                                                                                                             
      93               2 :         $this->fp = fopen($fileName, ($this->getAppend()? 'a':'w'));                                                        
      94               2 :         if($this->fp) {                                                                                                     
      95               2 :             if(flock($this->fp, LOCK_EX)) {                                                                                 
      96               2 :                 if($this->getAppend()) {                                                                                    
      97               2 :                     fseek($this->fp, 0, SEEK_END);                                                                          
      98               2 :                 }                                                                                                           
      99               2 :                 fwrite($this->fp, $this->layout->getHeader());                                                              
     100               2 :                 flock($this->fp, LOCK_UN);                                                                                  
     101               2 :                 $this->closed = false;                                                                                      
     102               2 :             } else {                                                                                                        
     103                 :                 // TODO: should we take some action in this case?                                                           
     104               0 :                 $this->closed = true;                                                                                       
     105                 :             }                                                                                                               
     106               2 :         } else {                                                                                                            
     107               0 :             $this->closed = true;                                                                                           
     108                 :         }                                                                                                                   
     109               2 :     }                                                                                                                       
     110                 : }                                                                                                                           

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.