Final Class Yiisoft\Log\Target\File\FileRotator
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
FileRotator takes care of rotating files.
If the size of the file exceeds {@see \
All existing files are moved backwards by one place, i.e., '.2' to '.3', '.1' to '.2', and so on. If compression
is enabled {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| rotateFile() | Yiisoft\ |
|
| shouldRotateFile() | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| COMPRESS_EXTENSION | '.gz' | The extension of the compressed rotated files. | Yiisoft\ |
Method Details
| public mixed __construct ( integer $maxFileSize = 10240, integer $maxFiles = 5, integer|null $fileMode = null, boolean $compressRotatedFiles = false ) | ||
| $maxFileSize | integer |
The maximum file size, in kilobytes. Defaults to 10240, meaning 10MB. |
| $maxFiles | integer |
The number of files used for rotation. Defaults to 5. |
| $fileMode | integer|null |
The permission to be set for newly created files. This value will be used by PHP
|
| $compressRotatedFiles | boolean |
Whether to compress rotated files with gzip. |
public function __construct(
int $maxFileSize = 10240,
int $maxFiles = 5,
private ?int $fileMode = null,
bool $compressRotatedFiles = false,
) {
$this->checkCannotBeLowerThanOne($maxFileSize, '$maxFileSize');
$this->checkCannotBeLowerThanOne($maxFiles, '$maxFiles');
$this->maxFileSize = $maxFileSize;
$this->maxFiles = $maxFiles;
if ($compressRotatedFiles && !extension_loaded('zlib')) {
throw new RuntimeException(sprintf(
'The %s requires the PHP extension "ext-zlib" to compress rotated files.',
self::class,
));
}
$this->compressRotatedFiles = $compressRotatedFiles;
}
| public void rotateFile ( string $file ) | ||
| $file | string | |
public function rotateFile(string $file): void
{
for ($i = $this->maxFiles; $i >= 0; --$i) {
// `$i === 0` is the original file
$rotateFile = $file . ($i === 0 ? '' : '.' . $i);
$newFile = $file . '.' . ($i + 1);
if ($i === $this->maxFiles) {
$this->safeRemove($this->compressRotatedFiles ? $rotateFile . self::COMPRESS_EXTENSION : $rotateFile);
continue;
}
if ($this->compressRotatedFiles && is_file($rotateFile . self::COMPRESS_EXTENSION)) {
$this->rotate($rotateFile . self::COMPRESS_EXTENSION, $newFile . self::COMPRESS_EXTENSION);
continue;
}
if (!is_file($rotateFile)) {
continue;
}
$this->rotate($rotateFile, $newFile);
if ($i === 0) {
$this->clear($rotateFile);
}
}
}
| public boolean shouldRotateFile ( string $file ) | ||
| $file | string | |
public function shouldRotateFile(string $file): bool
{
return file_exists($file) && @filesize($file) > ($this->maxFileSize * 1024);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.