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: }
84:
85: // Check if rollover is needed
86: else if ($this->currentDate !== $eventDate) {
87: $this->currentDate = $eventDate;
88:
89: // Close the file if it's open.
90: // Note: $this->close() is not called here because it would set
91: // $this->closed to true and the appender would not recieve
92: // any more logging requests
93: if (is_resource($this->fp)) {
94: $this->write($this->layout->getFooter());
95: fclose($this->fp);
96: }
97: $this->fp = null;
98: }
99:
100: parent::append($event);
101: }
102:
103: /** Renders the date using the configured <var>datePattern<var>. */
104: protected function getDate($timestamp = null) {
105: return date($this->datePattern, $timestamp);
106: }
107:
108: /**
109: * Determines target file. Replaces %s in file path with a date.
110: */
111: protected function getTargetFile() {
112: return str_replace('%s', $this->currentDate, $this->file);
113: }
114:
115: /**
116: * Sets the 'datePattern' parameter.
117: * @param string $datePattern
118: */
119: public function setDatePattern($datePattern) {
120: $this->setString('datePattern', $datePattern);
121: }
122:
123: /**
124: * Returns the 'datePattern' parameter.
125: * @return string
126: */
127: public function getDatePattern() {
128: return $this->datePattern;
129: }
130: }
131: