library/log4php/appenders/LoggerAppenderDailyFile.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     4  * contributor license agreements. See the NOTICE file distributed with
       
     5  * this work for additional information regarding copyright ownership.
       
     6  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     7  * (the "License"); you may not use this file except in compliance with
       
     8  * the License. You may obtain a copy of the License at
       
     9  *
       
    10  *       http://www.apache.org/licenses/LICENSE-2.0
       
    11  *
       
    12  * Unless required by applicable law or agreed to in writing, software
       
    13  * distributed under the License is distributed on an "AS IS" BASIS,
       
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    15  * See the License for the specific language governing permissions and
       
    16  * limitations under the License.
       
    17  */
       
    18 
       
    19 /**
       
    20  * An Appender that automatically creates a new logfile each day.
       
    21  *
       
    22  * The file is rolled over once a day. That means, for each day a new file
       
    23  * is created. A formatted version of the date pattern is used as to create
       
    24  * the file name using the {@link PHP_MANUAL#sprintf} function.
       
    25  *
       
    26  * This appender uses a layout.
       
    27  *
       
    28  * ##Configurable parameters:##
       
    29  *
       
    30  * - **datePattern** - Format for the date in the file path, follows formatting
       
    31  *     rules used by the PHP date() function. Default value: "Ymd".
       
    32  * - **file** - Path to the target file. Should contain a %s which gets
       
    33  *     substituted by the date.
       
    34  * - **append** - If set to true, the appender will append to the file,
       
    35  *     otherwise the file contents will be overwritten. Defaults to true.
       
    36  *
       
    37  * @version $Revision: 1382274 $
       
    38  * @package log4php
       
    39  * @subpackage appenders
       
    40  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
       
    41  * @link http://logging.apache.org/log4php/docs/appenders/daily-file.html Appender documentation
       
    42  */
       
    43 class LoggerAppenderDailyFile extends LoggerAppenderFile {
       
    44 
       
    45     /**
       
    46      * The 'datePattern' parameter.
       
    47      * Determines how date will be formatted in file name.
       
    48      * @var string
       
    49      */
       
    50     protected $datePattern = "Ymd";
       
    51 
       
    52     /**
       
    53      * Current date which was used when opening a file.
       
    54      * Used to determine if a rollover is needed when the date changes.
       
    55      * @var string
       
    56      */
       
    57     protected $currentDate;
       
    58 
       
    59     /** Additional validation for the date pattern. */
       
    60     public function activateOptions() {
       
    61         parent::activateOptions();
       
    62 
       
    63         if (empty($this->datePattern)) {
       
    64             $this->warn("Required parameter 'datePattern' not set. Closing appender.");
       
    65             $this->closed = true;
       
    66             return;
       
    67         }
       
    68     }
       
    69 
       
    70     /**
       
    71      * Appends a logging event.
       
    72      *
       
    73      * If the target file changes because of passage of time (e.g. at midnight)
       
    74      * the current file is closed. A new file, with the new date, will be
       
    75      * opened by the write() method.
       
    76      */
       
    77     public function append(LoggerLoggingEvent $event) {
       
    78         $eventDate = $this->getDate($event->getTimestamp());
       
    79 
       
    80         // Initial setting of current date
       
    81         if (!isset($this->currentDate)) {
       
    82             $this->currentDate = $eventDate;
       
    83         } // Check if rollover is needed
       
    84         else if ($this->currentDate !== $eventDate) {
       
    85             $this->currentDate = $eventDate;
       
    86 
       
    87             // Close the file if it's open.
       
    88             // Note: $this->close() is not called here because it would set
       
    89             //       $this->closed to true and the appender would not recieve
       
    90             //       any more logging requests
       
    91             if (is_resource($this->fp)) {
       
    92                 $this->write($this->layout->getFooter());
       
    93                 fclose($this->fp);
       
    94             }
       
    95             $this->fp = null;
       
    96         }
       
    97 
       
    98         parent::append($event);
       
    99     }
       
   100 
       
   101     /** Renders the date using the configured <var>datePattern<var>. */
       
   102     protected function getDate($timestamp = null) {
       
   103         return date($this->datePattern, $timestamp);
       
   104     }
       
   105 
       
   106     /**
       
   107      * Determines target file. Replaces %s in file path with a date.
       
   108      */
       
   109     protected function getTargetFile() {
       
   110         return str_replace('%s', $this->currentDate, $this->file);
       
   111     }
       
   112 
       
   113     /**
       
   114      * Sets the 'datePattern' parameter.
       
   115      * @param string $datePattern
       
   116      */
       
   117     public function setDatePattern($datePattern) {
       
   118         $this->setString('datePattern', $datePattern);
       
   119     }
       
   120 
       
   121     /**
       
   122      * Returns the 'datePattern' parameter.
       
   123      * @return string
       
   124      */
       
   125     public function getDatePattern() {
       
   126         return $this->datePattern;
       
   127     }
       
   128 }