Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,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
Expand All @@ -42,14 +44,15 @@ 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;
}

if (null !== $this->formatter) {
$record = $record->withMessage($this->formatter->formatMessage($record));
$message = $record->getMessage();
if (null !== $this->getFormatter()) {
$message = $record->withMessage($this->formatter->formatMessage($record));
}

// ... doHandle
$this->doHandle($record, $message);
}
}
21 changes: 0 additions & 21 deletions src/SonsOfPHP/Component/Logger/Handler/ConsoleHandler.php

This file was deleted.

47 changes: 45 additions & 2 deletions src/SonsOfPHP/Component/Logger/Handler/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
19 changes: 0 additions & 19 deletions src/SonsOfPHP/Component/Logger/Handler/SocketHandler.php

This file was deleted.

19 changes: 17 additions & 2 deletions src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Logger\Tests\Handler;

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Logger\Context;
use SonsOfPHP\Component\Logger\Handler\FileHandler;
use SonsOfPHP\Component\Logger\Level;
use SonsOfPHP\Component\Logger\Record;
use SonsOfPHP\Contract\Logger\HandlerInterface;

/**
* @coversDefaultClass \SonsOfPHP\Component\Logger\Handler\FileHandler
*
* @uses \SonsOfPHP\Component\Logger\Handler\FileHandler
* @uses \SonsOfPHP\Component\Logger\Context
* @uses \SonsOfPHP\Component\Logger\Record
* @uses \SonsOfPHP\Component\Logger\Level
* @uses \SonsOfPHP\Component\Logger\Handler\AbstractHandler
*/
final class FileHandlerTest extends TestCase
{
public function setUp(): void
{
if (file_exists('/tmp/testing.log')) {
unlink('/tmp/testing.log');
}
}

/**
* @covers ::__construct
*/
public function testItHasTheCorrectInterface(): void
{
$handler = new FileHandler('/tmp/testing.log');

$this->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->assertFileDoesNotExist('/tmp/testing.log');
$handler->handle($record);
$this->assertFileExists('/tmp/testing.log');

unset($handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Logger\Tests\Handler;

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Logger\Context;
use SonsOfPHP\Component\Logger\Handler\StreamHandler;
use SonsOfPHP\Component\Logger\Level;
use SonsOfPHP\Component\Logger\Record;
use SonsOfPHP\Contract\Logger\HandlerInterface;

/**
* @coversDefaultClass \SonsOfPHP\Component\Logger\Handler\StreamHandler
*
* @uses \SonsOfPHP\Component\Logger\Handler\StreamHandler
* @uses \SonsOfPHP\Component\Logger\Context
* @uses \SonsOfPHP\Component\Logger\Record
* @uses \SonsOfPHP\Component\Logger\Level
* @uses \SonsOfPHP\Component\Logger\Handler\AbstractHandler
*/
final class StreamHandlerTest extends TestCase
{
public function setUp(): void
{
if (file_exists('/tmp/testing.log')) {
unlink('/tmp/testing.log');
}
}

/**
* @covers ::__construct
*/
public function testItHasTheCorrectInterface(): void
{
$handler = new StreamHandler(fopen('/tmp/testing.log', 'a'));

$this->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);
}
}