From 0d8d6fad4f6a2400bb59292583f7412bca11a4c0 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Sun, 16 Nov 2025 09:16:07 -0500 Subject: [PATCH] chore: add log level configuration and enhance logger tests --- config.php | 4 + .../LoggerServiceProvider.php | 3 +- tests/Events/Listeners/ConnectedTest.php | 19 ++++- tests/Log/LoggerTest.php | 74 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/config.php b/config.php index 81ef275..46129d9 100644 --- a/config.php +++ b/config.php @@ -4,6 +4,10 @@ use Siteworxpro\App\Helpers\Env; return [ + 'app' => [ + 'log_level' => Env::get('LOG_LEVEL', 'debug'), + ], + /** * The server configuration. */ diff --git a/src/Services/ServiceProviders/LoggerServiceProvider.php b/src/Services/ServiceProviders/LoggerServiceProvider.php index 512757a..1745bdb 100644 --- a/src/Services/ServiceProviders/LoggerServiceProvider.php +++ b/src/Services/ServiceProviders/LoggerServiceProvider.php @@ -6,6 +6,7 @@ namespace Siteworxpro\App\Services\ServiceProviders; use Illuminate\Support\ServiceProvider; use Siteworxpro\App\Log\Logger; +use Siteworxpro\App\Services\Facades\Config; /** * Class LoggerServiceProvider @@ -17,7 +18,7 @@ class LoggerServiceProvider extends ServiceProvider public function register(): void { $this->app->singleton(Logger::class, function () { - return new Logger(); + return new Logger(Config::get('app.log_level')); }); } } diff --git a/tests/Events/Listeners/ConnectedTest.php b/tests/Events/Listeners/ConnectedTest.php index 866698a..7c5a3a2 100644 --- a/tests/Events/Listeners/ConnectedTest.php +++ b/tests/Events/Listeners/ConnectedTest.php @@ -5,12 +5,29 @@ declare(strict_types=1); namespace Siteworxpro\Tests\Events\Listeners; use Illuminate\Database\Events\ConnectionEstablished; -use Illuminate\Database\Events\ConnectionEvent; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LogLevel; use Siteworxpro\App\Events\Listeners\Database\Connected; +use Siteworxpro\App\Log\Logger; use Siteworxpro\Tests\Unit; class ConnectedTest extends Unit { + /** + * @throws ContainerExceptionInterface + * @throws \ReflectionException + * @throws NotFoundExceptionInterface + */ + protected function setUp(): void + { + parent::setUp(); + + $inputBuffer = fopen('php://memory', 'r+'); + $logger = new Logger(LogLevel::DEBUG, $inputBuffer); + \Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()->bind(Logger::class, fn() => $logger); + } + public function testHandlesEvent() { $this->expectNotToPerformAssertions(); diff --git a/tests/Log/LoggerTest.php b/tests/Log/LoggerTest.php index 4de291b..e618d67 100644 --- a/tests/Log/LoggerTest.php +++ b/tests/Log/LoggerTest.php @@ -4,12 +4,18 @@ declare(strict_types=1); namespace Siteworxpro\Tests\Log; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LogLevel; use Siteworxpro\App\Log\Logger; use Siteworxpro\Tests\Unit; class LoggerTest extends Unit { + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ private function getLoggerWithBuffer(string $logLevel): array { $inputBuffer = fopen('php://memory', 'r+'); @@ -21,6 +27,10 @@ class LoggerTest extends Unit return stream_get_contents($inputBuffer, -1, 0); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ private function testLogLevel(string $level): void { [$logger, $inputBuffer] = $this->getLoggerWithBuffer($level); @@ -33,6 +43,10 @@ class LoggerTest extends Unit $this->assertEquals('value', $decoded['context']['key']); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ private function testLogLevelEmpty(string $configLevel, string $logLevel): void { [$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel); @@ -42,57 +56,101 @@ class LoggerTest extends Unit $this->assertEmpty($output); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsDebugMessageWhenLevelIsDebug(): void { $this->testLogLevel(LogLevel::DEBUG); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsInfoMessageWhenLevelIsInfo(): void { $this->testLogLevel(LogLevel::INFO); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsWarningMessageWhenLevelIsWarning(): void { $this->testLogLevel(LogLevel::WARNING); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsErrorMessageWhenLevelIsError(): void { $this->testLogLevel(LogLevel::ERROR); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsCriticalMessageWhenLevelIsCritical(): void { $this->testLogLevel(LogLevel::CRITICAL); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsAlertMessageWhenLevelIsAlert(): void { $this->testLogLevel(LogLevel::ALERT); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsEmergencyMessageWhenLevelIsEmergency(): void { $this->testLogLevel(LogLevel::EMERGENCY); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsNoticeMessageWhenLevelIsNotice(): void { $this->testLogLevel(LogLevel::NOTICE); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testDoesNotLogWhenMinimumLevelIsInfo(): void { $this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testDoesNotLogWhenMinimumLevelIsWarning(): void { $this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO); $this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG); } + /** + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface + */ public function testDoesNotLogWhenMinimumLevelIsError(): void { $this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG); @@ -100,12 +158,20 @@ class LoggerTest extends Unit $this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testDoesNotLogWhenMinimumLevelIsNotice(): void { $this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG); $this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsMessageWithEmptyContext(): void { [$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO); @@ -118,6 +184,10 @@ class LoggerTest extends Unit $this->assertEquals('Message without context', $decoded['message']); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsMessageWithComplexContext(): void { [$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO); @@ -135,6 +205,10 @@ class LoggerTest extends Unit $this->assertEquals('value', $decoded['context']['nested']['key']); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testLogsStringableMessage(): void { [$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);