You've already forked Php-Template
chore: add log level configuration and enhance logger tests
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m25s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 3m20s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m29s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m28s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 4m3s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 3m19s
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m25s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 3m20s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m29s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m28s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 4m3s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 3m19s
This commit is contained in:
@@ -4,6 +4,10 @@ use Siteworxpro\App\Helpers\Env;
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
|
'app' => [
|
||||||
|
'log_level' => Env::get('LOG_LEVEL', 'debug'),
|
||||||
|
],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The server configuration.
|
* The server configuration.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Log\Logger;
|
use Siteworxpro\App\Log\Logger;
|
||||||
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class LoggerServiceProvider
|
* Class LoggerServiceProvider
|
||||||
@@ -17,7 +18,7 @@ class LoggerServiceProvider extends ServiceProvider
|
|||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Logger::class, function () {
|
$this->app->singleton(Logger::class, function () {
|
||||||
return new Logger();
|
return new Logger(Config::get('app.log_level'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,29 @@ declare(strict_types=1);
|
|||||||
namespace Siteworxpro\Tests\Events\Listeners;
|
namespace Siteworxpro\Tests\Events\Listeners;
|
||||||
|
|
||||||
use Illuminate\Database\Events\ConnectionEstablished;
|
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\Events\Listeners\Database\Connected;
|
||||||
|
use Siteworxpro\App\Log\Logger;
|
||||||
use Siteworxpro\Tests\Unit;
|
use Siteworxpro\Tests\Unit;
|
||||||
|
|
||||||
class ConnectedTest extends 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()
|
public function testHandlesEvent()
|
||||||
{
|
{
|
||||||
$this->expectNotToPerformAssertions();
|
$this->expectNotToPerformAssertions();
|
||||||
|
|||||||
@@ -4,12 +4,18 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\Tests\Log;
|
namespace Siteworxpro\Tests\Log;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Psr\Log\LogLevel;
|
use Psr\Log\LogLevel;
|
||||||
use Siteworxpro\App\Log\Logger;
|
use Siteworxpro\App\Log\Logger;
|
||||||
use Siteworxpro\Tests\Unit;
|
use Siteworxpro\Tests\Unit;
|
||||||
|
|
||||||
class LoggerTest extends Unit
|
class LoggerTest extends Unit
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
private function getLoggerWithBuffer(string $logLevel): array
|
private function getLoggerWithBuffer(string $logLevel): array
|
||||||
{
|
{
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
$inputBuffer = fopen('php://memory', 'r+');
|
||||||
@@ -21,6 +27,10 @@ class LoggerTest extends Unit
|
|||||||
return stream_get_contents($inputBuffer, -1, 0);
|
return stream_get_contents($inputBuffer, -1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
private function testLogLevel(string $level): void
|
private function testLogLevel(string $level): void
|
||||||
{
|
{
|
||||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($level);
|
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($level);
|
||||||
@@ -33,6 +43,10 @@ class LoggerTest extends Unit
|
|||||||
$this->assertEquals('value', $decoded['context']['key']);
|
$this->assertEquals('value', $decoded['context']['key']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
private function testLogLevelEmpty(string $configLevel, string $logLevel): void
|
private function testLogLevelEmpty(string $configLevel, string $logLevel): void
|
||||||
{
|
{
|
||||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel);
|
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel);
|
||||||
@@ -42,57 +56,101 @@ class LoggerTest extends Unit
|
|||||||
$this->assertEmpty($output);
|
$this->assertEmpty($output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::DEBUG);
|
$this->testLogLevel(LogLevel::DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsInfoMessageWhenLevelIsInfo(): void
|
public function testLogsInfoMessageWhenLevelIsInfo(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::INFO);
|
$this->testLogLevel(LogLevel::INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsWarningMessageWhenLevelIsWarning(): void
|
public function testLogsWarningMessageWhenLevelIsWarning(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::WARNING);
|
$this->testLogLevel(LogLevel::WARNING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsErrorMessageWhenLevelIsError(): void
|
public function testLogsErrorMessageWhenLevelIsError(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::ERROR);
|
$this->testLogLevel(LogLevel::ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsCriticalMessageWhenLevelIsCritical(): void
|
public function testLogsCriticalMessageWhenLevelIsCritical(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::CRITICAL);
|
$this->testLogLevel(LogLevel::CRITICAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsAlertMessageWhenLevelIsAlert(): void
|
public function testLogsAlertMessageWhenLevelIsAlert(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::ALERT);
|
$this->testLogLevel(LogLevel::ALERT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsEmergencyMessageWhenLevelIsEmergency(): void
|
public function testLogsEmergencyMessageWhenLevelIsEmergency(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::EMERGENCY);
|
$this->testLogLevel(LogLevel::EMERGENCY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsNoticeMessageWhenLevelIsNotice(): void
|
public function testLogsNoticeMessageWhenLevelIsNotice(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevel(LogLevel::NOTICE);
|
$this->testLogLevel(LogLevel::NOTICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testDoesNotLogWhenMinimumLevelIsInfo(): void
|
public function testDoesNotLogWhenMinimumLevelIsInfo(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG);
|
$this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testDoesNotLogWhenMinimumLevelIsWarning(): void
|
public function testDoesNotLogWhenMinimumLevelIsWarning(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO);
|
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO);
|
||||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG);
|
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
*/
|
||||||
public function testDoesNotLogWhenMinimumLevelIsError(): void
|
public function testDoesNotLogWhenMinimumLevelIsError(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG);
|
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG);
|
||||||
@@ -100,12 +158,20 @@ class LoggerTest extends Unit
|
|||||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING);
|
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testDoesNotLogWhenMinimumLevelIsNotice(): void
|
public function testDoesNotLogWhenMinimumLevelIsNotice(): void
|
||||||
{
|
{
|
||||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG);
|
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG);
|
||||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO);
|
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsMessageWithEmptyContext(): void
|
public function testLogsMessageWithEmptyContext(): void
|
||||||
{
|
{
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||||
@@ -118,6 +184,10 @@ class LoggerTest extends Unit
|
|||||||
$this->assertEquals('Message without context', $decoded['message']);
|
$this->assertEquals('Message without context', $decoded['message']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsMessageWithComplexContext(): void
|
public function testLogsMessageWithComplexContext(): void
|
||||||
{
|
{
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||||
@@ -135,6 +205,10 @@ class LoggerTest extends Unit
|
|||||||
$this->assertEquals('value', $decoded['context']['nested']['key']);
|
$this->assertEquals('value', $decoded['context']['nested']['key']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
public function testLogsStringableMessage(): void
|
public function testLogsStringableMessage(): void
|
||||||
{
|
{
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
||||||
|
|||||||
Reference in New Issue
Block a user