Skip to content

Commit c1cb47a

Browse files
authoredDec 27, 2021
Merge pull request #20 from cesargb/fix-issue-8
fix issue #8
2 parents c22522b + e9091a5 commit c1cb47a

10 files changed

+20
-326
lines changed
 

‎README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,24 @@ Every time a file is rotated one of these events occurs:
114114

115115
### RotateWasSuccessful
116116

117-
`Cesargb\File\Rotate\Events\RotateWasSuccessful`
117+
`Cesargb\LaravelLog\Events\RotateWasSuccessful`
118118

119119
This event will be fired when rotated was successful.
120120

121121
It has two public properties:
122122

123-
* fileSource: the full path of file to rotate
124-
* fileRotated: the full path of file rotated
123+
* filename: the full path of file to rotate
124+
* filenameTarget: the full path of file rotated
125125

126126
### RotateHasFailed
127127

128-
`Cesargb\File\Rotate\Handlers\RotativeHandler`
128+
`Cesargb\LaravelLog\Events\RotativeHandler`
129129

130130
This event will be fired when an error occurs while rotated
131131

132132
It has two public properties:
133133

134-
* fileSource: the full path of file to rotate
134+
* filename: the full path of file to rotate
135135
* exception: an object that extends PHP's Exception class.
136136

137137
## About
@@ -150,10 +150,6 @@ composer test
150150

151151
Please see [UPGRADING](UPGRADING.md) for details.
152152

153-
## Knowledge Issues
154-
155-
* [#8](https://github.com/cesargb/laravel-logs-rotate/issues/8) While the file is being rotated, any record of another process may be lost.
156-
157153
## Contributing
158154

159155
Any contributions are welcome.

‎composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"illuminate/log": "^6.20|^8.0",
2525
"illuminate/support": "^6.20|^8.0",
2626
"monolog/monolog": "^2.0",
27-
"cesargb/php-log-rotation": "^2.3"
27+
"cesargb/php-log-rotation": "^2.4"
2828
},
2929
"require-dev": {
3030
"orchestra/testbench": "^4.18|^6.20",

‎src/Handlers/AbstractHandler.php

-134
This file was deleted.

‎src/Handlers/HandlerInterface.php

-8
This file was deleted.

‎src/Handlers/RotativeHandler.php

-109
This file was deleted.

‎src/Rotate.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Cesargb\LaravelLog\Events\RotateHasFailed;
66
use Cesargb\LaravelLog\Events\RotateWasSuccessful;
7+
use Cesargb\LaravelLog\Helpers\Log;
78
use Cesargb\Log\Rotation;
89

910
class Rotate
@@ -17,7 +18,13 @@ public function files(array $filenames)
1718

1819
public function file(string $filename, array $options = []): bool
1920
{
20-
return $this->buildRotateDefault($options)->rotate($filename);
21+
$result = $this->buildRotateDefault($options)->rotate($filename);
22+
23+
if ($result) {
24+
Log::closeHandlers();
25+
}
26+
27+
return $result;
2128
}
2229

2330
private function buildRotateDefault(array $options = []): Rotation

‎tests/Commands/RotateFileTest.php

-49
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,7 @@ public function itCanRotateFile()
3030
Event::assertDispatched(RotateWasSuccessful::class, 2);
3131

3232
$this->assertEquals($resultCode, 0);
33-
$this->assertEquals(filesize($file1), 0);
3433
$this->assertFileExists($file1.'.1.gz');
35-
$this->assertEquals(filesize($file2), 0);
3634
$this->assertFileExists($file2.'.1.gz');
3735
}
38-
39-
// /** @test **/
40-
// public function itCanRotateFileArchive()
41-
// {
42-
// $file1 = $this->tmpDir.'/file1';
43-
// $file2 = $this->tmpDir.'/file2';
44-
45-
// $resultCode = Artisan::call('rotate:files', [
46-
// '--file' => [$file1, $file2],
47-
// '--dir' => $this->tmpDir.'/archive',
48-
// ]);
49-
50-
// Event::assertDispatched(RotateWasSuccessful::class, 2);
51-
52-
// $this->assertEquals($resultCode, 0);
53-
// $this->assertEquals(filesize($file1), 0);
54-
// $this->assertFileExists(dirname($file1).'/archive/'.basename($file1).'.1.gz');
55-
// $this->assertEquals(filesize($file2), 0);
56-
// $this->assertFileExists(dirname($file2).'/archive/'.basename($file2).'.1.gz');
57-
// }
58-
59-
// /** @test **/
60-
// public function itCanRotateFileMax()
61-
// {
62-
// $file = $this->tmpDir.'/file1';
63-
64-
// for ($n = 0; $n < 5; ++$n) {
65-
// file_put_contents($file, 'test');
66-
67-
// $resultCode = Artisan::call('rotate:files', [
68-
// '--file' => [$file],
69-
// '--max-files' => 3,
70-
// ]);
71-
72-
// $this->assertEquals($resultCode, 0);
73-
// }
74-
75-
// Event::assertDispatched(RotateWasSuccessful::class, 5);
76-
77-
// $this->assertEquals(filesize($file), 0);
78-
79-
// for ($n = 1; $n < 4; ++$n) {
80-
// $this->assertFileExists($file.'.'.$n.'.gz');
81-
// }
82-
83-
// $this->assertFileNotExists($file.basename($file).'.4.gz');
84-
// }
8536
}

‎tests/Handlers/RotativeHandlerTest.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function testItCanRotateLogs()
2727
Event::assertDispatched(RotateWasSuccessful::class, 1);
2828

2929
$this->assertEquals($resultCode, 0);
30-
$this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0);
3130
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz');
3231
}
3332

@@ -44,7 +43,6 @@ public function testItCanRotateLogsWithoutcompress()
4443
Event::assertDispatched(RotateWasSuccessful::class, 1);
4544

4645
$this->assertEquals($resultCode, 0);
47-
$this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0);
4846
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.1');
4947
}
5048

@@ -54,18 +52,14 @@ public function testItCanRotateLogsWithMaxfiles()
5452

5553
$this->app['config']->set('rotate.log_compress_files', true);
5654

57-
for ($n = 0; $n < 10; $n++) {
55+
for ($n = 0; $n < 10; ++$n) {
5856
$this->writeLog();
5957

60-
$this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log'));
61-
6258
Artisan::call('rotate:logs');
6359
}
6460

6561
Event::assertDispatched(RotateWasSuccessful::class, 10);
6662

67-
$this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0);
68-
6963
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz');
7064
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.2.gz');
7165
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.3.gz');

‎tests/RotateTest.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ public function testItCanWriteLogAfterRotate()
115115
Event::assertDispatched(RotateWasSuccessful::class, 1);
116116

117117
$this->assertEquals($resultCode, 0);
118-
$this->assertFileExists(app()->storagePath().'/logs/laravel.log');
119118
$this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz');
120119

121120
$this->writeLog();
122121

123-
$this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log'));
122+
//$this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log'));
124123
}
125124

126125
public function testRotateForeingFiles()
@@ -139,10 +138,8 @@ public function testRotateForeingFiles()
139138

140139
$this->assertEquals($resultCode, 0);
141140

142-
$this->assertFileExists($file);
143141
$this->assertFileExists(storage_path('logs/foreing_file.log.1.gz'));
144142

145-
unlink($file);
146143
unlink(storage_path('logs/foreing_file.log.1.gz'));
147144
}
148145
}

‎tests/TestCase.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setUp(): void
2626

2727
$this->cleanLogs();
2828

29-
if (! file_exists($this->tmpDir)) {
29+
if (!file_exists($this->tmpDir)) {
3030
mkdir($this->tmpDir);
3131
}
3232
}
@@ -49,7 +49,7 @@ protected function cleanLogs()
4949
$filesToRemove = glob(dirname($fileLog).'/*');
5050

5151
foreach ($filesToRemove as $f) {
52-
if (is_file($f) && ! is_dir($f)) {
52+
if (is_file($f) && !is_dir($f)) {
5353
unlink($f);
5454
}
5555
}
@@ -58,15 +58,15 @@ protected function cleanLogs()
5858
$filesToRemove = glob($this->tmpDir.'/*');
5959

6060
foreach ($filesToRemove as $f) {
61-
if (is_file($f) && ! is_dir($f)) {
61+
if (is_file($f) && !is_dir($f)) {
6262
unlink($f);
6363
}
6464
}
6565

6666
$filesToRemove = glob($this->tmpDir.'/archive/*');
6767

6868
foreach ($filesToRemove as $f) {
69-
if (is_file($f) && ! is_dir($f)) {
69+
if (is_file($f) && !is_dir($f)) {
7070
unlink($f);
7171
}
7272
}

0 commit comments

Comments
 (0)
Please sign in to comment.