From ee93c208e1e742d0d2ccbbc48ab007f9e6b1a51d Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 20:29:41 -0500 Subject: [PATCH 1/6] Logger --- .../Logger/Handler/AbstractHandler.php | 7 ++- .../Component/Logger/Handler/FileHandler.php | 47 ++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php b/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php index 407c638f..453253ec 100644 --- a/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php +++ b/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php @@ -20,6 +20,8 @@ public function __construct( protected ?FormatterInterface $formatter = null, ) {} + //abstract public function doHandle(RecordInterface $record, string $message): void; + public function getFilter(): ?FilterInterface { return $this->filter ?? null; @@ -46,10 +48,11 @@ public function handle(RecordInterface $record): void return; } + $message = $record->getMessage(); if (null !== $this->formatter) { - $record = $record->withMessage($this->formatter->formatMessage($record)); + $message = $record->withMessage($this->formatter->formatMessage($record)); } - // ... doHandle + $this->doHandle($record, $message); } } diff --git a/src/SonsOfPHP/Component/Logger/Handler/FileHandler.php b/src/SonsOfPHP/Component/Logger/Handler/FileHandler.php index d487837a..0447648c 100644 --- a/src/SonsOfPHP/Component/Logger/Handler/FileHandler.php +++ b/src/SonsOfPHP/Component/Logger/Handler/FileHandler.php @@ -14,8 +14,51 @@ */ class FileHandler extends AbstractHandler implements HandlerInterface { - public function handle(RecordInterface $record): void + private bool $isOpen = false; + private $handle; + + public function __construct( + private string $filename, + ) {} + + public function doHandle(RecordInterface $record, string $message): void + { + $this->open(); + + $this->write($message); + } + + private function write(string $message): void + { + if (false === fwrite($this->handle, $message)) { + throw new \RuntimeException(sprintf('"%s" could not be written to', $this->filename)); + } + } + + private function open(): void + { + if ($this->isOpen) { + return; + } + + if (false === $this->handle = fopen($this->filename, 'a')) { + throw new \RuntimeException(sprintf('"%s" could not be opened', $this->filename)); + } + $this->isOpen = true; + } + + private function close(): void + { + if (!$this->isOpen) { + return; + } + + fclose($this->handle); + $this->isOpen = false; + } + + public function __destruct() { - throw new \RuntimeException('To Be Implemented'); + $this->close(); } } From 2e62cc9e64e9329972bd5b0283ea2e1270c93b9e Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 20:34:02 -0500 Subject: [PATCH 2/6] bug fix --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e610a95b..07772b6d 100644 --- a/composer.json +++ b/composer.json @@ -67,8 +67,7 @@ "psr/http-factory": "^1.0", "psr/cache": "^2.0 || ^3.0", "psr/simple-cache": "^3.0", - "psr/log": "^1.0 || ^2.0 || ^3.0", - "sonsofphp/logger-contract": "0.3.x-dev" + "psr/log": "^1.0 || ^2.0 || ^3.0" }, "replace": { "sonsofphp/bard": "self.version", From 991dd5b547c95c347b1a67b2616ee552684b0adb Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 20:55:59 -0500 Subject: [PATCH 3/6] that should be it --- .../Logger/Handler/AbstractHandler.php | 6 +- .../Logger/Handler/ConsoleHandler.php | 21 ------ .../Logger/Handler/SocketHandler.php | 19 ------ .../Logger/Handler/StreamHandler.php | 19 +++++- .../Logger/Tests/Handler/FileHandlerTest.php | 66 +++++++++++++++++++ .../Tests/Handler/StreamHandlerTest.php | 63 ++++++++++++++++++ 6 files changed, 149 insertions(+), 45 deletions(-) delete mode 100644 src/SonsOfPHP/Component/Logger/Handler/ConsoleHandler.php delete mode 100644 src/SonsOfPHP/Component/Logger/Handler/SocketHandler.php create mode 100644 src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php create mode 100644 src/SonsOfPHP/Component/Logger/Tests/Handler/StreamHandlerTest.php diff --git a/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php b/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php index 453253ec..d703bf81 100644 --- a/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php +++ b/src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php @@ -34,7 +34,7 @@ public function setFilter(FilterInterface $filter): void public function getFormatter(): ?FormatterInterface { - return $this->formatter; + return $this->formatter ?? null; } public function setFormatter(FormatterInterface $formatter): void @@ -44,12 +44,12 @@ public function setFormatter(FormatterInterface $formatter): void public function handle(RecordInterface $record): void { - if (null !== $this->filter && false === $this->filter->isLoggable($record)) { + if (null !== $this->getFilter() && false === $this->filter->isLoggable($record)) { return; } $message = $record->getMessage(); - if (null !== $this->formatter) { + if (null !== $this->getFormatter()) { $message = $record->withMessage($this->formatter->formatMessage($record)); } diff --git a/src/SonsOfPHP/Component/Logger/Handler/ConsoleHandler.php b/src/SonsOfPHP/Component/Logger/Handler/ConsoleHandler.php deleted file mode 100644 index dc3c26e2..00000000 --- a/src/SonsOfPHP/Component/Logger/Handler/ConsoleHandler.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class ConsoleHandler extends AbstractHandler implements HandlerInterface -{ - public function handle(RecordInterface $record): void - { - throw new \RuntimeException('To Be Implemented'); - } -} diff --git a/src/SonsOfPHP/Component/Logger/Handler/SocketHandler.php b/src/SonsOfPHP/Component/Logger/Handler/SocketHandler.php deleted file mode 100644 index b747c619..00000000 --- a/src/SonsOfPHP/Component/Logger/Handler/SocketHandler.php +++ /dev/null @@ -1,19 +0,0 @@ - - */ -class SocketHandler extends AbstractHandler implements HandlerInterface -{ - public function handle(RecordInterface $record): void - { - throw new \RuntimeException('To Be Implemented'); - } -} diff --git a/src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php b/src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php index 355d004a..3c11b2c2 100644 --- a/src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php +++ b/src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php @@ -12,8 +12,23 @@ */ class StreamHandler extends AbstractHandler implements HandlerInterface { - public function handle(RecordInterface $record): void + private bool $isOpen = false; + private $stream; + + public function __construct($stream) + { + $this->stream = $stream; + } + + public function doHandle(RecordInterface $record, string $message): void + { + $this->write($message); + } + + private function write(string $message): void { - throw new \RuntimeException('To Be Implemented'); + if (false === fwrite($this->stream, $message)) { + throw new \RuntimeException(sprintf('stream could not be written to')); + } } } diff --git a/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php new file mode 100644 index 00000000..430d9c01 --- /dev/null +++ b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php @@ -0,0 +1,66 @@ +assertInstanceOf(HandlerInterface::class, $handler); + } + + /** + * @covers ::doHandle + * @covers ::open + * @covers ::write + * @covers ::close + * @covers ::__destruct + */ + public function testItCanWrite(): void + { + $handler = new FileHandler('/tmp/testing.log'); + $record = new Record( + channel: 'app', + level: Level::Debug, + message: 'Example {key} Message', + context: new Context(['key' => 'value']), + datetime: new \DateTimeImmutable('2020-04-20T04:20:00+00:00'), + ); + + $this->assertFalse(file_exists('/tmp/testing.log')); + $handler->handle($record); + $this->assertTrue(file_exists('/tmp/testing.log')); + + unset($handler); + } +} diff --git a/src/SonsOfPHP/Component/Logger/Tests/Handler/StreamHandlerTest.php b/src/SonsOfPHP/Component/Logger/Tests/Handler/StreamHandlerTest.php new file mode 100644 index 00000000..bac296c9 --- /dev/null +++ b/src/SonsOfPHP/Component/Logger/Tests/Handler/StreamHandlerTest.php @@ -0,0 +1,63 @@ +assertInstanceOf(HandlerInterface::class, $handler); + } + + /** + * @covers ::doHandle + * @covers ::write + */ + public function testItCanWrite(): void + { + $handler = new StreamHandler(fopen('/tmp/testing.log', 'a')); + $record = new Record( + channel: 'app', + level: Level::Debug, + message: 'Example {key} Message', + context: new Context(['key' => 'value']), + datetime: new \DateTimeImmutable('2020-04-20T04:20:00+00:00'), + ); + + $this->assertSame('', file_get_contents('/tmp/testing.log')); + $handler->handle($record); + $this->assertNotSame('', file_get_contents('/tmp/testing.log')); + + unset($handler); + } +} From e019424b070bbf90c2efd1ca8d7c629f23f460c9 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 20:56:24 -0500 Subject: [PATCH 4/6] cs fixes --- .../Component/Logger/Tests/Handler/FileHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php index 430d9c01..6cf0caa5 100644 --- a/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php +++ b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php @@ -57,9 +57,9 @@ public function testItCanWrite(): void datetime: new \DateTimeImmutable('2020-04-20T04:20:00+00:00'), ); - $this->assertFalse(file_exists('/tmp/testing.log')); + $this->assertFileNotExists('/tmp/testing.log'); $handler->handle($record); - $this->assertTrue(file_exists('/tmp/testing.log')); + $this->assertFileExists('/tmp/testing.log'); unset($handler); } From 566d7c87a7f1f04bb51146233079c11a2e1060ad Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 20:57:33 -0500 Subject: [PATCH 5/6] docs --- mkdocs.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/mkdocs.yml b/mkdocs.yml index 34ffbf2e..4cb1d437 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -106,6 +106,7 @@ nav: - HttpFactory: components/http-factory/index.md - HttpMessage: components/http-message/index.md - JSON: components/json/index.md + - Logger: components/logger/index.md - Money: - components/money/index.md - Currency Providers: components/money/currency-providers.md From c9d104c9e11a7a881a9941e624645146a4ce2a98 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 13 Nov 2023 21:00:08 -0500 Subject: [PATCH 6/6] meh --- .../Component/Logger/Tests/Handler/FileHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php index 6cf0caa5..174785a6 100644 --- a/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php +++ b/src/SonsOfPHP/Component/Logger/Tests/Handler/FileHandlerTest.php @@ -57,7 +57,7 @@ public function testItCanWrite(): void datetime: new \DateTimeImmutable('2020-04-20T04:20:00+00:00'), ); - $this->assertFileNotExists('/tmp/testing.log'); + $this->assertFileDoesNotExist('/tmp/testing.log'); $handler->handle($record); $this->assertFileExists('/tmp/testing.log');