0 follower

Final Class Yiisoft\Log\Target\File\FileRotator

InheritanceYiisoft\Log\Target\File\FileRotator
ImplementsYiisoft\Log\Target\File\FileRotatorInterface

FileRotator takes care of rotating files.

If the size of the file exceeds {@see \Yiisoft\Log\Target\File\FileRotator::$maxFileSize} (in kilo-bytes), a rotation will be performed, which renames the current file by suffixing the file name with '.1'.

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 \Yiisoft\Log\Target\File\FileRotator::$compressRotatedFiles}, the rotated files will be compressed into the '.gz' format. The property {@see \Yiisoft\Log\Target\File\FileRotator::$maxFiles} specifies how many history files to keep.

Constants

Hide inherited constants

Constant Value Description Defined By
COMPRESS_EXTENSION '.gz' The extension of the compressed rotated files. Yiisoft\Log\Target\File\FileRotator

Method Details

Hide inherited methods

__construct() public method

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 chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.

$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;
}

            
rotateFile() public method

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);
        }
    }
}

            
shouldRotateFile() public method

public boolean shouldRotateFile ( string $file )
$file string

                public function shouldRotateFile(string $file): bool
{
    return file_exists($file) && @filesize($file) > ($this->maxFileSize * 1024);
}