cesargb/php-log-rotation

PHP Class to rotate log files

2.5.0 2022-01-25 22:54 UTC

This package is auto-updated.

Last update: 2022-06-30 16:49:16 UTC


README

PHP Class to rotate log files

This class permit log rotating with diferetne processor.

tests Latest Version on Packagist Scrutinizer Code Quality

Note: If you have the version 1 installed, read this.

Installation

You can install this package via composer using:

composer require cesargb/php-log-rotation

Usage

use Cesargb\Log\Rotation;
use Cesargb\Log\Exceptions\RotationFailed;

$rotation = new Rotation();

$rotation
    ->compress() // Optional, compress the file after rotated. Default false
    ->files(30) // Optional, files are rotated 30 times before being removed. Default 366
    ->minSize(1024) // Optional, are rotated when they grow bigger than 1024 bytes. Default 0
    ->truncate() // Optional, truncate the original log file in place after creating a copy, instead of moving the old log file.
    ->then(function ($filenameTarget, $filenameRotated) {}) // Optional, to get filename target and original filename
    ->catch(function (RotationFailed $exception) {}) // Optional, to catch a exception in rotating
    ->finally(function ($message, $filenameTarget) {}) // Optional, this method will be called when the process has finished
    ->rotate('file.log');

Or you can define the options in the constructor

use Cesargb\Log\Rotation;
use Cesargb\Log\Exceptions\RotationFailed;

$rotation = new Rotation([
    'files' => 1,
    'compress' => true,
    'min-size' => 10,
    'truncate' => false,
    'then' => function ($filename) {},
    'catch' => function (RotationFailed $exception) {},
    'finally' => function ($message, $filename) {},
]);

$rotation->rotate('file.log');

Test

Run test with:

composer test

Contributing

Any contributions are welcome.