diff --git a/CHANGELOG.md b/CHANGELOG.md index c3cb2ff..d97f5e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://git.d3data.de/D3Public/guzzleFactory/compare/1.0.0...rel_1.x) +### Added +- combined OXID logger and file logger + - errors are written here (expected behaviour) + - all error level are written to custom log file ## [1.0.0](https://git.d3data.de/D3Public/guzzleFactory/releases/tag/1.0.0) - 2025-01-01 ### Added diff --git a/README.md b/README.md index eec8273..c907e96 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ $guzzleFactory->setMessageFormatter( '{method} {uri} HTTP/{version} {req_body}'.PHP_EOL.'RESPONSE: {code} - {res_body}', ['myUsername', 'myPassword'] ); +$guzzleFactory->setMessageLevel(Logger::INFO); $httpClient = $guzzleFactory->getGuzzle('https://remoteApi.com'); ``` diff --git a/composer.json b/composer.json index 77732d8..b7750d0 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ }, "scripts": { "test": "./vendor/bin/phpunit --no-coverage", + "test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html=coverage", "check-style": "./vendor/bin/php-cs-fixer fix --verbose --dry-run", "fix-style": "./vendor/bin/php-cs-fixer fix --verbose", "check-code": "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress --ansi" diff --git a/src/Apps/OxidLoggerTrait.php b/src/Apps/OxidLoggerTrait.php index ed5b11d..f3d97ec 100644 --- a/src/Apps/OxidLoggerTrait.php +++ b/src/Apps/OxidLoggerTrait.php @@ -17,6 +17,10 @@ declare(strict_types=1); namespace D3\GuzzleFactory\Apps; +use Exception; +use InvalidArgumentException; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; use OxidEsales\Eshop\Core\Registry; use RuntimeException; @@ -31,6 +35,36 @@ trait OxidLoggerTrait $this->loggers['oxid'] = Registry::getLogger(); } + /** + * @throws Exception + * @throws InvalidArgumentException + */ + public function addCombinedOxidAndFileLogger( + string $loggerName, + string $filePath, + int $logLevel = Logger::INFO, + ?int $maxFiles = null + ): void + { + if (!class_exists(Registry::class)) { + throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); + } + + $logger = new Logger($loggerName); + $stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles); + $logger->pushHandler($stream_handler); + + $oxidLogFilePath = $this->getOxidLogPath('oxideshop.log'); + $oxidStreamHandler = new StreamHandler($oxidLogFilePath, Logger::ERROR); + $logger->pushHandler($oxidStreamHandler); + + if (isset($this->loggers['oxid'])) { + unset($this->loggers['oxid']); + } + + $this->loggers[$loggerName] = $logger; + } + public function getOxidLogPath(string $fileName): string { if (!class_exists(Registry::class)) { diff --git a/src/LoggerTrait.php b/src/LoggerTrait.php index f55559a..7cc324e 100644 --- a/src/LoggerTrait.php +++ b/src/LoggerTrait.php @@ -20,6 +20,7 @@ namespace D3\GuzzleFactory; use D3\GuzzleFactory\Apps\OxidLoggerTrait; use Exception; use InvalidArgumentException; +use Monolog\Handler\AbstractProcessingHandler; use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -37,17 +38,38 @@ trait LoggerTrait * @throws Exception * @throws InvalidArgumentException */ - public function addFileLogger(string $loggerName, string $filePath, int $logLevel = Logger::INFO, ?int $maxFiles = null): void + public function addFileLogger( + string $loggerName, + string $filePath, + int $logLevel = Logger::INFO, + ?int $maxFiles = null + ): void { $logger = new Logger($loggerName); - $stream_handler = is_null($maxFiles) ? - new StreamHandler($filePath, $logLevel) : - new RotatingFileHandler($filePath, $maxFiles, $logLevel); + $stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles); $logger->pushHandler($stream_handler); $this->loggers[$loggerName] = $logger; } + /** + * @param string $filePath + * @param int $logLevel + * @param int|null $maxFiles + * @return AbstractProcessingHandler + * @throws Exception + */ + public function getFileLoggerStreamHandler( + string $filePath, + int $logLevel = Logger::INFO, + ?int $maxFiles = null + ): AbstractProcessingHandler + { + return is_null($maxFiles) ? + new StreamHandler($filePath, $logLevel) : + new RotatingFileHandler($filePath, $maxFiles, $logLevel); + } + public function addConfiguredLogger(LoggerInterface $logger): void { $this->loggers[md5(serialize($logger))] = $logger; diff --git a/tests/Apps/OxidLoggerTestTrait.php b/tests/Apps/OxidLoggerTestTrait.php index 29e58d9..46b397a 100644 --- a/tests/Apps/OxidLoggerTestTrait.php +++ b/tests/Apps/OxidLoggerTestTrait.php @@ -40,6 +40,25 @@ trait OxidLoggerTestTrait ); } + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger + */ + public function testAddCombinedOxidAndFileLoggerWithoutOxid(): void + { + $sut = GuzzleFactory::create(); + + $this->expectException(RuntimeException::class); + + $this->callMethod( + $sut, + 'addCombinedOxidAndFileLogger', + ['nameFixture', 'file/path.log', 1, 5] + ); + } + /** * @test * @return void @@ -84,6 +103,32 @@ trait OxidLoggerTestTrait $this->assertInstanceOf(Logger::class, $loggers['oxid']); } + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger + */ + public function testAddCombinedOxidAndFileLoggerInOxid(): void + { + require_once __DIR__.'/../Helpers/classAliases.php'; + + $sut = GuzzleFactory::create(); + + $this->setValue($sut, 'loggers', ['oxid' => 1]); + + $this->callMethod( + $sut, + 'addCombinedOxidAndFileLogger', + ['nameFixture', 'file/path.log', 1, 5] + ); + + $loggers = $this->getValue($sut, 'loggers'); + $this->assertArrayHasKey('nameFixture', $loggers); + $this->assertArrayNotHasKey('oxid', $loggers); + $this->assertInstanceOf(Logger::class, $loggers['nameFixture']); + } + /** * @test * @return void diff --git a/tests/LoggerTestTrait.php b/tests/LoggerTestTrait.php index 3e47e12..e217bae 100644 --- a/tests/LoggerTestTrait.php +++ b/tests/LoggerTestTrait.php @@ -28,6 +28,7 @@ trait LoggerTestTrait * @test * @throws ReflectionException * @covers \D3\GuzzleFactory\GuzzleFactory::addFileLogger + * @covers \D3\GuzzleFactory\GuzzleFactory::getFileLoggerStreamHandler * @dataProvider addFileLoggerDataProvider */ public function testAddFileLogger(int $logLevel, ?int $maxFiles, string $expectedHandlerClass): void