Skip to content

Commit 4f6f8ef

Browse files
authored
Add argument to define the level of compression (#19)
1 parent 35250de commit 4f6f8ef

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use Cesargb\Log\Exceptions\RotationFailed;
2727
$rotation = new Rotation();
2828

2929
$rotation
30-
->compress() // Optional, compress the file after rotated. Default false
30+
->compress() // Optional, compress the file after rotated. Accept level compression argument.
3131
->files(30) // Optional, files are rotated 30 times before being removed. Default 366
3232
->minSize(1024) // Optional, are rotated when they grow bigger than 1024 bytes. Default 0
3333
->truncate() // Optional, truncate the original log file in place after creating a copy, instead of moving the old log file.
@@ -45,7 +45,7 @@ use Cesargb\Log\Exceptions\RotationFailed;
4545

4646
$rotation = new Rotation([
4747
'files' => 1,
48-
'compress' => true,
48+
'compress' => true, // Set level compression or true to default level. Default false
4949
'min-size' => 10,
5050
'truncate' => false,
5151
'then' => function ($filename) {},

src/Compress/Gz.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Gz
88
{
99
public const EXTENSION_COMPRESS = 'gz';
1010

11-
public function handler(string $filename): string
11+
public function handler(string $filename, ?int $level = null): string
1212
{
1313
$filenameCompress = $filename.'.'.self::EXTENSION_COMPRESS;
1414

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

21-
$gz = gzopen($filenameCompress, 'wb');
21+
$level = $level ?? '';
22+
23+
$gz = gzopen($filenameCompress, "wb{$level}");
2224

2325
if ($gz === false) {
2426
fclose($fd);

src/Rotation.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ class Rotation
1111
use Optionable;
1212
use ErrorHandler;
1313

14+
private const COMPRESS_DEFAULT_LEVEL = null;
15+
1416
private RotativeProcessor $processor;
1517

1618
private bool $_compress = false;
19+
private ?int $_compressLevel = self::COMPRESS_DEFAULT_LEVEL;
1720

1821
private int $_minSize = 0;
1922

@@ -52,11 +55,14 @@ public function files(int $count): self
5255
/**
5356
* Old versions of log files are compressed.
5457
*/
55-
public function compress(bool $compress = true): self
58+
public function compress(bool|int $level = true): self
5659
{
57-
$this->_compress = $compress;
60+
$this->_compress = (bool)($level);
61+
$this->_compressLevel = is_numeric($level)
62+
? $level
63+
: self::COMPRESS_DEFAULT_LEVEL;
5864

59-
if ($compress) {
65+
if ($this->_compress) {
6066
$this->processor->addExtension('gz');
6167
} else {
6268
$this->processor->removeExtention('gz');
@@ -149,7 +155,7 @@ private function runCompress(string $filename): ?string
149155
$gz = new Gz();
150156

151157
try {
152-
return $gz->handler($filename);
158+
return $gz->handler($filename, $this->_compressLevel);
153159
} catch (Exception $error) {
154160
$this->exception($error);
155161

tests/Compress/GzTest.php

+60-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,68 @@ public function testRotationProcessorWithGzProcessor(): void
2727
$this->assertEquals(self::DIR_WORK.'file.log.1.gz', $fileRotated);
2828
})->rotate(self::DIR_WORK.'file.log');
2929

30-
// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');
31-
3230
$this->assertFileExists(self::DIR_WORK.'file.log.1.gz');
3331

3432
$this->assertEquals($content, implode('', (array)gzfile(self::DIR_WORK.'file.log.1.gz')));
3533
}
34+
35+
public function testRotationProcessorWithGzProcessorWithLevel(): void
36+
{
37+
$rotation = new Rotation();
38+
39+
$rotation->compress();
40+
41+
$content = bin2hex('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
42+
Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
43+
ffffffffffffffff
44+
natoque penatibus et magnis dis parturient montes, nascetur
45+
hhhhhhhhhhhhhhhh
46+
ridiculus mus. Donec quam felis, ultricies nec, pellentesque
47+
ffffffhhhhhggggx x
48+
eu, pretium quis, sem. Nulla consequat massa quis enim.
49+
Donec pede justo, fringilla vel, aliquet nec, vulputate
50+
eget, arcu.');
51+
52+
$content .= $content;
53+
54+
55+
file_put_contents(self::DIR_WORK.'file.log', $content);
56+
$rotation->rotate(self::DIR_WORK.'file.log');
57+
$sizeDefaultLevel = filesize(self::DIR_WORK.'file.log.1.gz');
58+
59+
file_put_contents(self::DIR_WORK.'file.log', $content);
60+
$rotation->compress(1)->rotate(self::DIR_WORK.'file.log');
61+
$sizeMinLevel = filesize(self::DIR_WORK.'file.log.1.gz');
62+
63+
file_put_contents(self::DIR_WORK.'file.log', $content);
64+
$rotation->compress(9)->rotate(self::DIR_WORK.'file.log');
65+
$sizeMaxLevel = filesize(self::DIR_WORK.'file.log.1.gz');
66+
67+
$this->assertLessThan($sizeMinLevel, $sizeDefaultLevel);
68+
$this->assertGreaterThan($sizeMaxLevel, $sizeDefaultLevel);
69+
}
70+
71+
public function testRotationProcessorWithoutGzProcessorIfLevelIsZero(): void
72+
{
73+
$rotation = new Rotation();
74+
75+
$rotation->compress(0);
76+
77+
$content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
78+
Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
79+
natoque penatibus et magnis dis parturient montes, nascetur
80+
ridiculus mus. Donec quam felis, ultricies nec, pellentesque
81+
eu, pretium quis, sem. Nulla consequat massa quis enim.
82+
Donec pede justo, fringilla vel, aliquet nec, vulputate
83+
eget, arcu.';
84+
85+
file_put_contents(self::DIR_WORK.'file.log', $content);
86+
87+
$rotation->rotate(self::DIR_WORK.'file.log');
88+
89+
$this->assertFileExists(self::DIR_WORK.'file.log.1');
90+
$this->assertFileDoesNotExist(self::DIR_WORK.'file.log.1.gz');
91+
92+
$this->assertEquals($content, file_get_contents(self::DIR_WORK.'file.log.1'));
93+
}
3694
}

tests/Processors/RotativeProcessorTest.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public function testRotationProcessor(): void
2020
$rotation->rotate(self::DIR_WORK.'file.log');
2121
}
2222

23-
// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');
24-
2523
foreach (range(1, $maxFiles) as $n) {
2624
$this->assertFileExists(self::DIR_WORK.'file.log.'.$n);
2725
}
@@ -43,8 +41,6 @@ public function testRotationProcessorWithGzProcessor(): void
4341
$rotation->rotate(self::DIR_WORK.'file.log');
4442
}
4543

46-
// $this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');
47-
4844
foreach (range(1, $maxFiles) as $n) {
4945
$this->assertFileExists(self::DIR_WORK."file.log.{$n}.gz");
5046
}
@@ -53,4 +49,41 @@ public function testRotationProcessorWithGzProcessor(): void
5349

5450
$this->assertFalse(is_file(self::DIR_WORK."file.log.{$numeralCleaned}.gz"));
5551
}
52+
53+
public function testRotationProcessorWithGzProcessorWithLevel(): void
54+
{
55+
$tests = [
56+
[
57+
'level' => 0,
58+
'assert' => 'assertStringEndsNotWith',
59+
],
60+
[
61+
'level' => false,
62+
'assert' => 'assertStringEndsNotWith',
63+
],
64+
[
65+
'level' => true,
66+
'assert' => 'assertStringEndsWith',
67+
],
68+
[
69+
'level' => 5,
70+
'assert' => 'assertStringEndsWith',
71+
],
72+
];
73+
74+
$rotation = new Rotation();
75+
76+
foreach ($tests as $test) {
77+
$level = $test['level'];
78+
$assert = $test['assert'];
79+
file_put_contents(self::DIR_WORK.'file.log', microtime(true));
80+
81+
$rotation->compress($level)->then(function ($fileRotated) use ($assert) {
82+
$this->{$assert}('gz', $fileRotated);
83+
})->rotate(self::DIR_WORK.'file.log');
84+
}
85+
86+
87+
88+
}
5689
}

0 commit comments

Comments
 (0)