mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-22 00:56:08 +02:00
plug #245 log rotate with setTimedFilename
This commit is contained in:
@@ -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 = '%1$s-%2$s';
|
||||||
|
$this->dateFormat = 'Y-m-d';
|
||||||
|
|
||||||
parent::__construct($this->getTimedFilename(), $level, $bubble);
|
parent::__construct($this->getTimedFilename(), $level, $bubble);
|
||||||
}
|
}
|
||||||
@@ -88,12 +92,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 +113,38 @@ 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 = sprintf(
|
||||||
|
$fileInfo['dirname'] . '/' . $this->filenameFormat,
|
||||||
|
$fileInfo['filename'],
|
||||||
|
date($this->dateFormat)
|
||||||
|
);
|
||||||
|
|
||||||
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 = sprintf(
|
||||||
|
$fileInfo['dirname'] . '/' . $this->filenameFormat,
|
||||||
|
$fileInfo['filename'],
|
||||||
|
'*'
|
||||||
|
);
|
||||||
|
if (!empty($fileInfo['extension'])) {
|
||||||
|
$glob .= '.'.$fileInfo['extension'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $glob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTimedFilename($filenameFormat, $dateFormat, $glob)
|
||||||
|
{
|
||||||
|
$this->filenameFormat = $filenameFormat;
|
||||||
|
$this->dateFormat = $dateFormat;
|
||||||
|
$this->glob = $glob;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user