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: 2 additions & 1 deletion src/batch-symfony-framework/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"yokai/batch": "^0.5.0"
"yokai/batch": "^0.5.0",
"psr/log": "^1.0|^2.0|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 27 additions & 0 deletions src/batch-symfony-framework/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ The job launcher that will be injected depends on the packages you have installe
- if `yokai/batch-symfony-console` is installed, you will receive a `Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher`
- otherwise you will receive a `Yokai\Batch\Launcher\SimpleJobLauncher`


## Use the batchLogger
The aim of the batchLogger is to store log inside the jobExecution.
In a symfony project, you can use the batchLogger with the symfony autowiring by naming your variable as `$yokaiBatchLogger`

```php
<?php

namespace App;

use Psr\Log\LoggerInterface;

final readonly class YourService
{
public function __construct(
private LoggerInterface $yokaiBatchLogger,
) {
}

public function method()
{
$this->yokaiBatchLogger->error(...);
}
}
```
```

## On the same subject

- [What is a job execution storage ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/job-execution-storage.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection;

use Composer\InstalledVersions;
use Psr\Log\LoggerInterface;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader as ConfigLoader;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function load(array $configs, ContainerBuilder $container): void
JobLauncherInterface::class,
\array_keys(\array_filter($launchers))[0] ?? 'yokai_batch.job_launcher.simple'
);
$container->registerAliasForArgument('yokai_batch.logger', LoggerInterface::class, 'yokaiBatchLogger');
}

private function installed(string $package): bool
Expand Down
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>
48 changes: 48 additions & 0 deletions src/batch/src/Logger/BatchLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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);
}
}
40 changes: 40 additions & 0 deletions src/batch/tests/Logger/BatchLoggerTest.php
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());
}
}
12 changes: 10 additions & 2 deletions tests/symfony/src/Job/Country/CountryJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yokai\Batch\Sources\Tests\Symfony\App\Job\Country;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
Expand Down Expand Up @@ -56,14 +57,18 @@ final class CountryJob extends AbstractDecoratedJob implements
private ItemWriterInterface $writer;
private array $countries = [];
private bool $flushed = false;
private LoggerInterface $yokaiBatchLogger;

public static function getJobName(): string
{
return 'country';
}

public function __construct(JobExecutionStorageInterface $executionStorage, KernelInterface $kernel)
{
public function __construct(
JobExecutionStorageInterface $executionStorage,
KernelInterface $kernel,
LoggerInterface $yokaiBatchLogger
) {
$writePath = fn(string $format) => new StaticValueParameterAccessor(
ARTIFACT_DIR . '/symfony/country/countries.' . $format
);
Expand All @@ -81,6 +86,7 @@ public function __construct(JobExecutionStorageInterface $executionStorage, Kern
new FlatFileWriter($writePath('csv'), null, null, $headers),
new JsonLinesWriter($writePath('jsonl')),
]);
$this->yokaiBatchLogger = $yokaiBatchLogger;

parent::__construct(
new ItemJob(
Expand All @@ -95,6 +101,8 @@ public function __construct(JobExecutionStorageInterface $executionStorage, Kern

public function process(mixed $item): array
{
$this->yokaiBatchLogger->log('info', 'log process');

return ['iso2' => $item['code'], $item['_key'] => $item['value']];
}

Expand Down
1 change: 1 addition & 0 deletions tests/symfony/tests/CountryJobSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static function (JobExecution $execution) {
'{"iso2":"GB","iso3":"GBR","name":"United Kingdom","continent":"EU","currency":"GBP","phone":"44"}',
$jsonl
);
Assert::assertStringContainsString('log process', $execution->getLogs()->__toString());
},
];
}
Expand Down