diff --git a/library/log4php/appenders/LoggerAppenderDailyFile.php b/library/log4php/appenders/LoggerAppenderDailyFile.php new file mode 100644 --- /dev/null +++ b/library/log4php/appenders/LoggerAppenderDailyFile.php @@ -0,0 +1,128 @@ +datePattern)) { + $this->warn("Required parameter 'datePattern' not set. Closing appender."); + $this->closed = true; + return; + } + } + + /** + * Appends a logging event. + * + * If the target file changes because of passage of time (e.g. at midnight) + * the current file is closed. A new file, with the new date, will be + * opened by the write() method. + */ + public function append(LoggerLoggingEvent $event) { + $eventDate = $this->getDate($event->getTimestamp()); + + // Initial setting of current date + if (!isset($this->currentDate)) { + $this->currentDate = $eventDate; + } // Check if rollover is needed + else if ($this->currentDate !== $eventDate) { + $this->currentDate = $eventDate; + + // Close the file if it's open. + // Note: $this->close() is not called here because it would set + // $this->closed to true and the appender would not recieve + // any more logging requests + if (is_resource($this->fp)) { + $this->write($this->layout->getFooter()); + fclose($this->fp); + } + $this->fp = null; + } + + parent::append($event); + } + + /** Renders the date using the configured datePattern. */ + protected function getDate($timestamp = null) { + return date($this->datePattern, $timestamp); + } + + /** + * Determines target file. Replaces %s in file path with a date. + */ + protected function getTargetFile() { + return str_replace('%s', $this->currentDate, $this->file); + } + + /** + * Sets the 'datePattern' parameter. + * @param string $datePattern + */ + public function setDatePattern($datePattern) { + $this->setString('datePattern', $datePattern); + } + + /** + * Returns the 'datePattern' parameter. + * @return string + */ + public function getDatePattern() { + return $this->datePattern; + } +}