Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create react and echo log handlers #1

Open
wants to merge 1 commit into
base: 1.x.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
"name": "kpicaza"
}
],
"repositories": [
{
"type": "github",
"url": "[email protected]:WyriHaximus-labs/filesystem.git"
}
],
"require": {
"php": ">=8.1",
"monolog/monolog": "^2.3",
"react/filesystem": "rewrite-dev",
"webmozart/assert": "^1.10"
},
"require-dev": {
Expand Down
69 changes: 69 additions & 0 deletions src/ReactFilesystemHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Antidot\Async\Logger;

use Monolog\Handler\AbstractProcessingHandler;
use Psr\Log\LogLevel;
use React\EventLoop\LoopInterface;
use React\Filesystem\Factory;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\Node\NotExistInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use Webmozart\Assert\Assert;
use const FILE_APPEND;

/**
* @psalm-import-type Level from \Monolog\Logger
* @psalm-import-type LevelName from \Monolog\Logger
* @psalm-import-type FormattedRecord from AbstractProcessingHandler
*/
final class ReactFilesystemHandler extends AbstractProcessingHandler
{
private PromiseInterface $filesystem;
private string $logPath;
private LoopInterface $loop;

/**
* @psalm-param Level|LevelName|LogLevel::* $debug
*/
public function __construct(LoopInterface $loop, string $logPath, int|string $debug, bool $bubble)
{
$this->loop = $loop;
$this->logPath = $logPath;
$filesystem = Factory::create();
/** @psalm-suppress TooManyTemplateParams */
$this->filesystem = $filesystem->detect($this->logPath)
->then(static function (NotExistInterface $node): PromiseInterface {
/** @psalm-suppress TooManyTemplateParams */
return $node->createFile();
});

parent::__construct($debug, $bubble);
}

/**
* @param FormattedRecord $record
*/
protected function write(array $record): void
{
$this->loop->futureTick(function () use ($record) {
$this->filesystem
->then(static function (FileInterface $file) use ($record): PromiseInterface {
$formatted = $record['formatted'];
Assert::string($formatted);
/** @psalm-suppress TooManyTemplateParams */
return $file->putContents($formatted, FILE_APPEND);
});
});
}

public function close(): void
{
if ($this->filesystem instanceof ExtendedPromiseInterface) {
$this->filesystem->done();
}
}
}
32 changes: 32 additions & 0 deletions test/ReactFileSystemHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Antidot\Test\Async\Logger;

use Antidot\Async\Logger\ReactFilesystemHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use React\EventLoop\Loop;

final class ReactFileSystemHandlerTest extends TestCase
{
const LOG_PATH = 'test/example.log';

protected function tearDown(): void
{
unlink(self::LOG_PATH);
}

public function testItWritesLogInFutureTick(): void
{
$loop = Loop::get();
$logger = new Logger('default');
$handler = new ReactFilesystemHandler($loop, self::LOG_PATH, Logger::DEBUG, true);
$logger->pushHandler($handler);
$logger->debug('Hola Mundo');
$loop->run();
$log = file_get_contents(self::LOG_PATH);
self::assertStringContainsString('Hola Mundo', $log);
}
}