Skip to content

Add argument to define the level of compression #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use Cesargb\Log\Exceptions\RotationFailed;
$rotation = new Rotation();

$rotation
->compress() // Optional, compress the file after rotated. Default false
->compress() // Optional, compress the file after rotated. Accept level compression argument.
->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.
Expand All @@ -45,7 +45,7 @@ use Cesargb\Log\Exceptions\RotationFailed;

$rotation = new Rotation([
'files' => 1,
'compress' => true,
'compress' => true, // Set level compression or true to default level. Default false
'min-size' => 10,
'truncate' => false,
'then' => function ($filename) {},
Expand Down
6 changes: 4 additions & 2 deletions src/Compress/Gz.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Gz
{
public const EXTENSION_COMPRESS = 'gz';

public function handler(string $filename): string
public function handler(string $filename, ?int $level = null): string
{
$filenameCompress = $filename.'.'.self::EXTENSION_COMPRESS;

Expand All @@ -18,7 +18,9 @@ public function handler(string $filename): string
throw new Exception("file {$filename} not can read.", 100);
}

$gz = gzopen($filenameCompress, 'wb');
$level = $level ?? '';

$gz = gzopen($filenameCompress, "wb{$level}");

if ($gz === false) {
fclose($fd);
Expand Down
14 changes: 10 additions & 4 deletions src/Rotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class Rotation
use Optionable;
use ErrorHandler;

private const COMPRESS_DEFAULT_LEVEL = null;

private RotativeProcessor $processor;

private bool $_compress = false;
private ?int $_compressLevel = self::COMPRESS_DEFAULT_LEVEL;

private int $_minSize = 0;

Expand Down Expand Up @@ -52,11 +55,14 @@ public function files(int $count): self
/**
* Old versions of log files are compressed.
*/
public function compress(bool $compress = true): self
public function compress(bool|int $level = true): self
{
$this->_compress = $compress;
$this->_compress = (bool)($level);
$this->_compressLevel = is_numeric($level)
? $level
: self::COMPRESS_DEFAULT_LEVEL;

if ($compress) {
if ($this->_compress) {
$this->processor->addExtension('gz');
} else {
$this->processor->removeExtention('gz');
Expand Down Expand Up @@ -149,7 +155,7 @@ private function runCompress(string $filename): ?string
$gz = new Gz();

try {
return $gz->handler($filename);
return $gz->handler($filename, $this->_compressLevel);
} catch (Exception $error) {
$this->exception($error);

Expand Down
62 changes: 60 additions & 2 deletions tests/Compress/GzTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,68 @@ public function testRotationProcessorWithGzProcessor(): void
$this->assertEquals(self::DIR_WORK.'file.log.1.gz', $fileRotated);
})->rotate(self::DIR_WORK.'file.log');

// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');

$this->assertFileExists(self::DIR_WORK.'file.log.1.gz');

$this->assertEquals($content, implode('', (array)gzfile(self::DIR_WORK.'file.log.1.gz')));
}

public function testRotationProcessorWithGzProcessorWithLevel(): void
{
$rotation = new Rotation();

$rotation->compress();

$content = bin2hex('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
ffffffffffffffff
natoque penatibus et magnis dis parturient montes, nascetur
hhhhhhhhhhhhhhhh
ridiculus mus. Donec quam felis, ultricies nec, pellentesque
ffffffhhhhhggggx x
eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate
eget, arcu.');

$content .= $content;


file_put_contents(self::DIR_WORK.'file.log', $content);
$rotation->rotate(self::DIR_WORK.'file.log');
$sizeDefaultLevel = filesize(self::DIR_WORK.'file.log.1.gz');

file_put_contents(self::DIR_WORK.'file.log', $content);
$rotation->compress(1)->rotate(self::DIR_WORK.'file.log');
$sizeMinLevel = filesize(self::DIR_WORK.'file.log.1.gz');

file_put_contents(self::DIR_WORK.'file.log', $content);
$rotation->compress(9)->rotate(self::DIR_WORK.'file.log');
$sizeMaxLevel = filesize(self::DIR_WORK.'file.log.1.gz');

$this->assertLessThan($sizeMinLevel, $sizeDefaultLevel);
$this->assertGreaterThan($sizeMaxLevel, $sizeDefaultLevel);
}

public function testRotationProcessorWithoutGzProcessorIfLevelIsZero(): void
{
$rotation = new Rotation();

$rotation->compress(0);

$content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
natoque penatibus et magnis dis parturient montes, nascetur
ridiculus mus. Donec quam felis, ultricies nec, pellentesque
eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate
eget, arcu.';

file_put_contents(self::DIR_WORK.'file.log', $content);

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

$this->assertFileExists(self::DIR_WORK.'file.log.1');
$this->assertFileDoesNotExist(self::DIR_WORK.'file.log.1.gz');

$this->assertEquals($content, file_get_contents(self::DIR_WORK.'file.log.1'));
}
}
41 changes: 37 additions & 4 deletions tests/Processors/RotativeProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public function testRotationProcessor(): void
$rotation->rotate(self::DIR_WORK.'file.log');
}

// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');

foreach (range(1, $maxFiles) as $n) {
$this->assertFileExists(self::DIR_WORK.'file.log.'.$n);
}
Expand All @@ -43,8 +41,6 @@ public function testRotationProcessorWithGzProcessor(): void
$rotation->rotate(self::DIR_WORK.'file.log');
}

// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');

foreach (range(1, $maxFiles) as $n) {
$this->assertFileExists(self::DIR_WORK."file.log.{$n}.gz");
}
Expand All @@ -53,4 +49,41 @@ public function testRotationProcessorWithGzProcessor(): void

$this->assertFalse(is_file(self::DIR_WORK."file.log.{$numeralCleaned}.gz"));
}

public function testRotationProcessorWithGzProcessorWithLevel(): void
{
$tests = [
[
'level' => 0,
'assert' => 'assertStringEndsNotWith',
],
[
'level' => false,
'assert' => 'assertStringEndsNotWith',
],
[
'level' => true,
'assert' => 'assertStringEndsWith',
],
[
'level' => 5,
'assert' => 'assertStringEndsWith',
],
];

$rotation = new Rotation();

foreach ($tests as $test) {
$level = $test['level'];
$assert = $test['assert'];
file_put_contents(self::DIR_WORK.'file.log', microtime(true));

$rotation->compress($level)->then(function ($fileRotated) use ($assert) {
$this->{$assert}('gz', $fileRotated);
})->rotate(self::DIR_WORK.'file.log');
}



}
}