1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 21:26:43 +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 $mustRotate;
protected $nextRotation;
protected $filenameFormat;
protected $dateFormat;
/**
* @param string $filename
@@ -40,6 +42,8 @@ class RotatingFileHandler extends StreamHandler
$this->filename = $filename;
$this->maxFiles = (int) $maxFiles;
$this->nextRotation = new \DateTime('tomorrow');
$this->filenameFormat = '{filename}-{date}';
$this->dateFormat = 'Y-m-d';
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}
*/
@@ -88,12 +98,7 @@ class RotatingFileHandler extends StreamHandler
return;
}
$fileInfo = pathinfo($this->filename);
$glob = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-*';
if (!empty($fileInfo['extension'])) {
$glob .= '.'.$fileInfo['extension'];
}
$logFiles = glob($glob);
$logFiles = glob($this->getGlobPattern());
if ($this->maxFiles >= count($logFiles)) {
// no files to remove
return;
@@ -114,11 +119,31 @@ class RotatingFileHandler extends StreamHandler
protected function getTimedFilename()
{
$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'])) {
$timedFilename .= '.'.$fileInfo['extension'];
}
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;
}
}