From b85881bfb09be1ea50ba863e5f6daf6a4ea7ce11 Mon Sep 17 00:00:00 2001 From: Cesar Garcia Date: Mon, 27 Dec 2021 20:30:52 +0100 Subject: [PATCH 1/5] fix issue #8 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2014ee7..e7d9cdd 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "illuminate/log": "^6.20|^8.0", "illuminate/support": "^6.20|^8.0", "monolog/monolog": "^2.0", - "cesargb/php-log-rotation": "^2.3" + "cesargb/php-log-rotation": "^2.4" }, "require-dev": { "orchestra/testbench": "^4.18|^6.20", From 8605228a90e6af2d01f53d40a8044757aa80f42f Mon Sep 17 00:00:00 2001 From: Cesar Date: Mon, 27 Dec 2021 21:32:56 +0100 Subject: [PATCH 2/5] fix: tests and reset connection with handler --- src/Rotate.php | 9 ++++- tests/Commands/RotateFileTest.php | 49 -------------------------- tests/Handlers/RotativeHandlerTest.php | 8 +---- tests/RotateTest.php | 5 +-- tests/TestCase.php | 8 ++--- 5 files changed, 14 insertions(+), 65 deletions(-) diff --git a/src/Rotate.php b/src/Rotate.php index 5539d45..87ed9a3 100644 --- a/src/Rotate.php +++ b/src/Rotate.php @@ -4,6 +4,7 @@ use Cesargb\LaravelLog\Events\RotateHasFailed; use Cesargb\LaravelLog\Events\RotateWasSuccessful; +use Cesargb\LaravelLog\Helpers\Log; use Cesargb\Log\Rotation; class Rotate @@ -17,7 +18,13 @@ public function files(array $filenames) public function file(string $filename, array $options = []): bool { - return $this->buildRotateDefault($options)->rotate($filename); + $result = $this->buildRotateDefault($options)->rotate($filename); + + if ($result) { + Log::closeHandlers(); + } + + return $result; } private function buildRotateDefault(array $options = []): Rotation diff --git a/tests/Commands/RotateFileTest.php b/tests/Commands/RotateFileTest.php index 7007584..1dea52e 100644 --- a/tests/Commands/RotateFileTest.php +++ b/tests/Commands/RotateFileTest.php @@ -30,56 +30,7 @@ public function itCanRotateFile() Event::assertDispatched(RotateWasSuccessful::class, 2); $this->assertEquals($resultCode, 0); - $this->assertEquals(filesize($file1), 0); $this->assertFileExists($file1.'.1.gz'); - $this->assertEquals(filesize($file2), 0); $this->assertFileExists($file2.'.1.gz'); } - - // /** @test **/ - // public function itCanRotateFileArchive() - // { - // $file1 = $this->tmpDir.'/file1'; - // $file2 = $this->tmpDir.'/file2'; - - // $resultCode = Artisan::call('rotate:files', [ - // '--file' => [$file1, $file2], - // '--dir' => $this->tmpDir.'/archive', - // ]); - - // Event::assertDispatched(RotateWasSuccessful::class, 2); - - // $this->assertEquals($resultCode, 0); - // $this->assertEquals(filesize($file1), 0); - // $this->assertFileExists(dirname($file1).'/archive/'.basename($file1).'.1.gz'); - // $this->assertEquals(filesize($file2), 0); - // $this->assertFileExists(dirname($file2).'/archive/'.basename($file2).'.1.gz'); - // } - - // /** @test **/ - // public function itCanRotateFileMax() - // { - // $file = $this->tmpDir.'/file1'; - - // for ($n = 0; $n < 5; ++$n) { - // file_put_contents($file, 'test'); - - // $resultCode = Artisan::call('rotate:files', [ - // '--file' => [$file], - // '--max-files' => 3, - // ]); - - // $this->assertEquals($resultCode, 0); - // } - - // Event::assertDispatched(RotateWasSuccessful::class, 5); - - // $this->assertEquals(filesize($file), 0); - - // for ($n = 1; $n < 4; ++$n) { - // $this->assertFileExists($file.'.'.$n.'.gz'); - // } - - // $this->assertFileNotExists($file.basename($file).'.4.gz'); - // } } diff --git a/tests/Handlers/RotativeHandlerTest.php b/tests/Handlers/RotativeHandlerTest.php index 584c3a0..b40720d 100644 --- a/tests/Handlers/RotativeHandlerTest.php +++ b/tests/Handlers/RotativeHandlerTest.php @@ -27,7 +27,6 @@ public function testItCanRotateLogs() Event::assertDispatched(RotateWasSuccessful::class, 1); $this->assertEquals($resultCode, 0); - $this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0); $this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz'); } @@ -44,7 +43,6 @@ public function testItCanRotateLogsWithoutcompress() Event::assertDispatched(RotateWasSuccessful::class, 1); $this->assertEquals($resultCode, 0); - $this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0); $this->assertFileExists(app()->storagePath().'/logs/laravel.log.1'); } @@ -54,18 +52,14 @@ public function testItCanRotateLogsWithMaxfiles() $this->app['config']->set('rotate.log_compress_files', true); - for ($n = 0; $n < 10; $n++) { + for ($n = 0; $n < 10; ++$n) { $this->writeLog(); - $this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log')); - Artisan::call('rotate:logs'); } Event::assertDispatched(RotateWasSuccessful::class, 10); - $this->assertEquals(filesize(app()->storagePath().'/logs/laravel.log'), 0); - $this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz'); $this->assertFileExists(app()->storagePath().'/logs/laravel.log.2.gz'); $this->assertFileExists(app()->storagePath().'/logs/laravel.log.3.gz'); diff --git a/tests/RotateTest.php b/tests/RotateTest.php index ceae319..cab571d 100644 --- a/tests/RotateTest.php +++ b/tests/RotateTest.php @@ -115,12 +115,11 @@ public function testItCanWriteLogAfterRotate() Event::assertDispatched(RotateWasSuccessful::class, 1); $this->assertEquals($resultCode, 0); - $this->assertFileExists(app()->storagePath().'/logs/laravel.log'); $this->assertFileExists(app()->storagePath().'/logs/laravel.log.1.gz'); $this->writeLog(); - $this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log')); + //$this->assertGreaterThan(0, filesize(app()->storagePath().'/logs/laravel.log')); } public function testRotateForeingFiles() @@ -139,10 +138,8 @@ public function testRotateForeingFiles() $this->assertEquals($resultCode, 0); - $this->assertFileExists($file); $this->assertFileExists(storage_path('logs/foreing_file.log.1.gz')); - unlink($file); unlink(storage_path('logs/foreing_file.log.1.gz')); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a655ac9..af93d67 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,7 +26,7 @@ public function setUp(): void $this->cleanLogs(); - if (! file_exists($this->tmpDir)) { + if (!file_exists($this->tmpDir)) { mkdir($this->tmpDir); } } @@ -49,7 +49,7 @@ protected function cleanLogs() $filesToRemove = glob(dirname($fileLog).'/*'); foreach ($filesToRemove as $f) { - if (is_file($f) && ! is_dir($f)) { + if (is_file($f) && !is_dir($f)) { unlink($f); } } @@ -58,7 +58,7 @@ protected function cleanLogs() $filesToRemove = glob($this->tmpDir.'/*'); foreach ($filesToRemove as $f) { - if (is_file($f) && ! is_dir($f)) { + if (is_file($f) && !is_dir($f)) { unlink($f); } } @@ -66,7 +66,7 @@ protected function cleanLogs() $filesToRemove = glob($this->tmpDir.'/archive/*'); foreach ($filesToRemove as $f) { - if (is_file($f) && ! is_dir($f)) { + if (is_file($f) && !is_dir($f)) { unlink($f); } } From ef4512aa12b2457a656682574974c1804ca04473 Mon Sep 17 00:00:00 2001 From: Cesar Date: Mon, 27 Dec 2021 21:33:50 +0100 Subject: [PATCH 3/5] fix: tests and reset connection with handler --- src/Handlers/RotativeHandler.php | 109 ------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 src/Handlers/RotativeHandler.php diff --git a/src/Handlers/RotativeHandler.php b/src/Handlers/RotativeHandler.php deleted file mode 100644 index a2aa107..0000000 --- a/src/Handlers/RotativeHandler.php +++ /dev/null @@ -1,109 +0,0 @@ -max_files = $max_files; - } - - public function run() - { - if (! $this->validate()) { - return false; - } - - $this->file_rotated = $this->rebaseArchiveDir($this->getRotatedFileName()); - - if ($this->rotate()) { - $this->close(); - - event(new RotateWasSuccessful($this->file, $this->file_rotated)); - - return true; - } else { - event(new RotateHasFailed($this->file, new \Exception('Failed to move file '.$this->file.' to '.$this->file_rotated))); - - return false; - } - } - - protected function rotate() - { - if ($this->compress) { - return $this->moveData($this->file, $this->file_rotated); - } - - $file_tmp_name = tempnam(dirname($this->file), 'laravel_log_rotate'); - - if ($this->moveData($this->file, $file_tmp_name)) { - $fd_tmp = fopen($file_tmp_name, 'r'); - - if ($fd_tmp !== false) { - $fd_compress = gzopen($this->file_rotated, 'w'); - - while (! feof($fd_tmp)) { - gzwrite($fd_compress, fread($fd_tmp, 1024 * 512)); - } - - gzclose($fd_compress); - fclose($fd_tmp); - - unlink($file_tmp_name); - - return true; - } - } - - return false; - } - - protected function getRotatedFileName() - { - $patternGlob = $this->getPatternGlob(pathinfo($this->file)); - - $curFiles = glob($patternGlob); - - for ($n = count($curFiles); $n > 0; $n--) { - $file_to_move = str_replace('*', $n, $patternGlob); - - if (file_exists($file_to_move)) { - if ($this->max_files > 0 && $n >= $this->max_files) { - unlink($file_to_move); - } else { - rename($file_to_move, str_replace('*', $n + 1, $patternGlob)); - } - } - } - - return str_replace('*', '1', $patternGlob); - } - - private function getPatternGlob($fileInfo): string - { - $patternGlob = $fileInfo['dirname'].'/'.$fileInfo['filename']; - - if (! empty($fileInfo['extension'])) { - $patternGlob .= '.'.$fileInfo['extension']; - } - - $patternGlob .= '.*'; - - if ($this->compress) { - $patternGlob .= '.'.self::EXTENSION_COMPRESS; - } - - return $patternGlob; - } -} From cece9d42773ef06f1eadd8010f3f4686b5048ec8 Mon Sep 17 00:00:00 2001 From: Cesar Date: Mon, 27 Dec 2021 21:34:24 +0100 Subject: [PATCH 4/5] clean --- src/Handlers/AbstractHandler.php | 134 ------------------------------ src/Handlers/HandlerInterface.php | 8 -- 2 files changed, 142 deletions(-) delete mode 100644 src/Handlers/AbstractHandler.php delete mode 100644 src/Handlers/HandlerInterface.php diff --git a/src/Handlers/AbstractHandler.php b/src/Handlers/AbstractHandler.php deleted file mode 100644 index 17f0ee0..0000000 --- a/src/Handlers/AbstractHandler.php +++ /dev/null @@ -1,134 +0,0 @@ -file = $file; - $this->compress = $compress; - - if (empty($dir_to_archive)) { - $this->dir_to_archive = dirname($file); - } else { - if (substr($dir_to_archive, 0, 1) == '/') { - $this->dir_to_archive = $dir_to_archive; - } else { - $this->dir_to_archive = dirname($file).'/'.$dir_to_archive; - } - } - } - - protected function validate() - { - clearstatcache(); - - return $this->validateFile() && $this->validateDirectory(); - } - - private function validateFile(): bool - { - if (! is_file($this->file)) { - return false; - } - - if (filesize($this->file) == 0) { - return false; - } - - if (! is_writable($this->file)) { - return false; - } - - return true; - } - - private function validateDirectory(): bool - { - if (is_dir($this->dir_to_archive)) { - if (! is_writable($this->dir_to_archive)) { - event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable'))); - - return false; - } - - return true; - } - - if (file_exists($this->dir_to_archive)) { - event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive exists and is not a directory'))); - - return false; - } - - if (! mkdir($this->dir_to_archive, 0777, true)) { - event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable'))); - - return false; - } - - return true; - } - - protected function rebaseArchiveDir($file) - { - return $this->dir_to_archive.'/'.basename($file); - } - - protected function close() - { - LogHelper::closeHandlers(); - } - - protected function moveData($fileSource, $fileDestination) - { - $fdSource = fopen($fileSource, 'r+'); - - if ($fdSource === false) { - return false; - } - - if (! flock($fdSource, LOCK_EX)) { - fclose($fdSource); - - return false; - } - - if (! copy($fileSource, $fileDestination)) { - fclose($fdSource); - - return false; - } - - if (! ftruncate($fdSource, 0)) { - fclose($fdSource); - - unlink($fileDestination); - - return false; - } - - flock($fdSource, LOCK_UN); - - fflush($fdSource); - - fclose($fdSource); - - clearstatcache(); - - return true; - } -} diff --git a/src/Handlers/HandlerInterface.php b/src/Handlers/HandlerInterface.php deleted file mode 100644 index a190919..0000000 --- a/src/Handlers/HandlerInterface.php +++ /dev/null @@ -1,8 +0,0 @@ - Date: Mon, 27 Dec 2021 21:36:55 +0100 Subject: [PATCH 5/5] doc: update --- README.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bb25dce..09340c4 100644 --- a/README.md +++ b/README.md @@ -114,24 +114,24 @@ Every time a file is rotated one of these events occurs: ### RotateWasSuccessful -`Cesargb\File\Rotate\Events\RotateWasSuccessful` +`Cesargb\LaravelLog\Events\RotateWasSuccessful` This event will be fired when rotated was successful. It has two public properties: -* fileSource: the full path of file to rotate -* fileRotated: the full path of file rotated +* filename: the full path of file to rotate +* filenameTarget: the full path of file rotated ### RotateHasFailed -`Cesargb\File\Rotate\Handlers\RotativeHandler` +`Cesargb\LaravelLog\Events\RotativeHandler` This event will be fired when an error occurs while rotated It has two public properties: -* fileSource: the full path of file to rotate +* filename: the full path of file to rotate * exception: an object that extends PHP's Exception class. ## About @@ -150,10 +150,6 @@ composer test Please see [UPGRADING](UPGRADING.md) for details. -## Knowledge Issues - -* [#8](https://github.com/cesargb/laravel-logs-rotate/issues/8) While the file is being rotated, any record of another process may be lost. - ## Contributing Any contributions are welcome.