126 parent::__construct($filename,
'a', $delay);
127 $this->when = strtoupper(
$when);
130 $this->rolloverAt = null;
131 $this->dayOfWeek = null;
133 if ($this->when ==
'S') {
135 $this->suffix =
'%Y-%m-%d_%H-%M-%S';
136 $this->extMatch =
'^\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}$';
137 } elseif ($this->when ==
'M') {
138 $this->interval = 60;
139 $this->suffix =
'%Y-%m-%d_%H-%M';
140 $this->extMatch =
'^\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}$';
141 } elseif ($this->when ==
'H') {
142 $this->interval = 60 * 60;
143 $this->suffix =
'%Y-%m-%d_%H';
144 $this->extMatch =
'^\\d{4}-\\d{2}-\\d{2}_\\d{2}$';
145 } elseif ($this->when ==
'D' || $this->when ==
'MIDNIGHT') {
146 $this->interval = 60 * 60 * 24;
147 $this->suffix =
'%Y-%m-%d';
148 $this->extMatch =
'^\\d{4}-\\d{2}-\\d{2}$';
149 } elseif (substr($this->when, 0, 1) ==
'W') {
150 $this->interval = 60 * 60 * 24 * 7;
151 if (strlen($this->when) != 2) {
152 throw new \Plop\Exception(
154 'You must specify a day for weekly rollover '.
155 'from 0 to 6 (0 is Monday), not %s',
161 $day = ord($this->when[1]) - ord(
'0');
162 if ($day < 0 || $day > 6) {
163 throw new \Plop\Exception(
165 'Invalid day specified for weekly rollover: %s',
171 $this->dayOfWeek = $day;
172 $this->suffix =
'%Y-%m-%d';
173 $this->extMatch =
'^\\d{4}-\\d{2}-\\d{2}$';
175 throw new \Plop\Exception(
177 'Invalid rollover interval specified: %s',
184 throw new \Plop\Exception(
185 'The interval should be an integer ' .
186 'greater than or equal to 1' 190 $this->interval = $this->interval *
$interval;
206 if ($this->when ==
'MIDNIGHT') {
207 return strtotime(
"midnight + 1 day", $currentTime);
210 if (substr($this->when, 0, 1) ==
'W') {
212 "next " . self::$dayNames[$this->dayOfWeek],
236 return ($this->
getTime() >= $this->rolloverAt);
247 $dirName = dirname($this->baseFilename);
248 $baseName = basename($this->baseFilename);
249 $fileNames = scandir($dirName);
251 $prefix = $baseName .
'.';
252 $plen = strlen($prefix);
254 foreach ($fileNames as $fileName) {
255 if ($fileName ==
'.' || $fileName ==
'..') {
259 if (!strncmp($fileName, $prefix, $plen)) {
260 $suffix = substr($fileName, $plen);
261 if (preg_match($this->extMatch,
$suffix)) {
262 $result[] = $dirName . DIRECTORY_SEPARATOR . $fileName;
268 $rlen = count($result);
269 if ($rlen < $this->backupCount) {
272 $result = array_slice($result, 0, $rlen - $this->backupCount);
280 if (is_resource($this->stream)) {
281 fclose($this->stream);
287 $formatFunc =
'gmstrftime';
289 $formatFunc =
'strftime';
292 $dfn = $this->baseFilename .
'.' . $formatFunc($this->suffix, $t);
293 if (file_exists($dfn)) {
297 rename($this->baseFilename, $dfn);
298 if ($this->backupCount > 0) {
305 $this->stream = $this->
open();
306 $currentTime = time();
308 while ($newRolloverAt <= $currentTime) {
311 $this->rolloverAt = $newRolloverAt;
$dayOfWeek
Day of week (0=Monday...6=Sunday) when the log rotation happens.
shouldRollover(\Plop\RecordInterface $record)
Interface for a log record.
__construct($filename, $when= 'h', $interval=1, $backupCount=0, $delay=false, $utc=false)
$utc
Whether the files are named based on UTC time or local time.
static $dayNames
The names of days in English, starting with Monday.
$extMatch
A PCRE pattern that matches rotated files.
$when
Log rotation specification (eg. 'W' or 'MIDNIGHT').
$backupCount
Number of backup log files to keep.
$suffix
The date format that will be used as a suffix for the log files.
computeRollover($currentTime)
An abstract class for handlers that must deal with file rotations.
An handler that writes logs to a file but also handles that file's rotation, based on time...
$interval
Interval between file rotations.
$rolloverAt
UNIX timestamp of the next log rotation.