1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-09 22:56:41 +02:00

Merge pull request #267 from cordoval/feature/logrotate_flexible

plug #245 log rotate with setTimedFilename
This commit is contained in:
Jordi Boggiano
2013-11-11 02:42:16 -08:00

View File

@@ -28,6 +28,8 @@ class RotatingFileHandler extends StreamHandler
protected $maxFiles; protected $maxFiles;
protected $mustRotate; protected $mustRotate;
protected $nextRotation; protected $nextRotation;
protected $filenameFormat;
protected $dateFormat;
/** /**
* @param string $filename * @param string $filename
@@ -40,6 +42,8 @@ class RotatingFileHandler extends StreamHandler
$this->filename = $filename; $this->filename = $filename;
$this->maxFiles = (int) $maxFiles; $this->maxFiles = (int) $maxFiles;
$this->nextRotation = new \DateTime('tomorrow'); $this->nextRotation = new \DateTime('tomorrow');
$this->filenameFormat = '{filename}-{date}';
$this->dateFormat = 'Y-m-d';
parent::__construct($this->getTimedFilename(), $level, $bubble); parent::__construct($this->getTimedFilename(), $level, $bubble);
} }
@@ -56,6 +60,12 @@ class RotatingFileHandler extends StreamHandler
} }
} }
public function setFilenameFormat($filenameFormat, $dateFormat)
{
$this->filenameFormat = $filenameFormat;
$this->dateFormat = $dateFormat;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@@ -88,12 +98,7 @@ class RotatingFileHandler extends StreamHandler
return; return;
} }
$fileInfo = pathinfo($this->filename); $logFiles = glob($this->getGlobPattern());
$glob = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-*';
if (!empty($fileInfo['extension'])) {
$glob .= '.'.$fileInfo['extension'];
}
$logFiles = glob($glob);
if ($this->maxFiles >= count($logFiles)) { if ($this->maxFiles >= count($logFiles)) {
// no files to remove // no files to remove
return; return;
@@ -114,11 +119,31 @@ class RotatingFileHandler extends StreamHandler
protected function getTimedFilename() protected function getTimedFilename()
{ {
$fileInfo = pathinfo($this->filename); $fileInfo = pathinfo($this->filename);
$timedFilename = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-'.date('Y-m-d'); $timedFilename = str_replace(
array('{filename}', '{date}'),
array($fileInfo['filename'], date($this->dateFormat)),
$fileInfo['dirname'] . '/' . $this->filenameFormat
);
if (!empty($fileInfo['extension'])) { if (!empty($fileInfo['extension'])) {
$timedFilename .= '.'.$fileInfo['extension']; $timedFilename .= '.'.$fileInfo['extension'];
} }
return $timedFilename; return $timedFilename;
} }
protected function getGlobPattern()
{
$fileInfo = pathinfo($this->filename);
$glob = str_replace(
array('{filename}', '{date}'),
array($fileInfo['filename'], '*'),
$fileInfo['dirname'] . '/' . $this->filenameFormat
);
if (!empty($fileInfo['extension'])) {
$glob .= '.'.$fileInfo['extension'];
}
return $glob;
}
} }