-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #90 add global logger service that logs in job
- Loading branch information
Showing
7 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/batch-symfony-framework/src/Resources/services/global/logger.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<defaults public="false"/> | ||
|
||
<service id="yokai_batch.logger" class="Yokai\Batch\Logger\BatchLogger"> | ||
<tag name="kernel.event_listener" method="onPreExecute" event="Yokai\Batch\Event\PreExecuteEvent"/> | ||
<tag name="kernel.event_listener" method="onPostExecute" event="Yokai\Batch\Event\PostExecuteEvent"/> | ||
</service> | ||
|
||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/* | ||
* This file is part of the Tucania project. | ||
* | ||
* Copyright (C) Tucania (https://www.tucania.com/) - All Rights Reserved | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Logger; | ||
|
||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\InvalidArgumentException; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Stringable; | ||
use Yokai\Batch\Event\PostExecuteEvent; | ||
use Yokai\Batch\Event\PreExecuteEvent; | ||
|
||
/** | ||
* BatchLogger allow to log with the jobExecutionLogger | ||
*/ | ||
class BatchLogger extends AbstractLogger | ||
{ | ||
private ?LoggerInterface $batchLogger = null; | ||
|
||
/** | ||
* Access and remember the logger | ||
*/ | ||
public function onPreExecute(PreExecuteEvent $event): void | ||
{ | ||
$this->batchLogger = $event->getExecution()->getLogger(); | ||
} | ||
|
||
/** | ||
* Forget the logger | ||
*/ | ||
public function onPostExecute(PostExecuteEvent $event): void | ||
{ | ||
$this->batchLogger = null; | ||
} | ||
|
||
/** | ||
* Log with the batchLogger defined in the PreExecuteEvent or with nullLogger if nothing remembered | ||
* | ||
* @param array<string, mixed> $context | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function log($level, Stringable|string $message, array $context = []): void | ||
{ | ||
($this->batchLogger ?? new NullLogger())->log($level, $message, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Logger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yokai\Batch\Event\PostExecuteEvent; | ||
use Yokai\Batch\Event\PreExecuteEvent; | ||
use Yokai\Batch\JobExecution; | ||
use Yokai\Batch\Logger\BatchLogger; | ||
use Yokai\Batch\Tests\Dummy\DebugEventDispatcher; | ||
|
||
class BatchLoggerTest extends TestCase | ||
{ | ||
public function testLaunch(): void | ||
{ | ||
$dispatcher = new DebugEventDispatcher(); | ||
$logger = new BatchLogger(); | ||
|
||
$dispatcher->addListener(PreExecuteEvent::class, [$logger, 'onPreExecute']); | ||
$dispatcher->addListener(PostExecuteEvent::class, [$logger, 'onPostExecute']); | ||
|
||
$execution = JobExecution::createRoot('123', 'test.job_executor'); | ||
|
||
$logger->log('info', 'before'); | ||
$preExecuteEvent = new PreExecuteEvent($execution); | ||
$dispatcher->dispatch($preExecuteEvent); | ||
|
||
$logger->log('info', 'between'); | ||
|
||
$postExecuteEvent = new PostExecuteEvent($execution); | ||
$dispatcher->dispatch($postExecuteEvent); | ||
$logger->log('info', 'after'); | ||
|
||
self::assertStringNotContainsString('before', $execution->getLogs()->__toString()); | ||
self::assertStringContainsString('between', $execution->getLogs()->__toString()); | ||
self::assertStringNotContainsString('after', $execution->getLogs()->__toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters