Skip to content

Commit 83e5a19

Browse files
authored
feature #90 - Add global logger service that logs in JobExecution (#116)
* feature #90 add global logger service that logs in job * #90 code review - update documentation & psr/log required version
1 parent 6c9dbab commit 83e5a19

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"symfony/dependency-injection": "^6.4|^7.0",
1919
"symfony/http-kernel": "^6.4|^7.0",
2020
"symfony/framework-bundle": "^6.4|^7.0",
21-
"yokai/batch": "^0.5.0"
21+
"yokai/batch": "^0.5.0",
22+
"psr/log": "^1.0|^2.0|^3.0"
2223
},
2324
"autoload": {
2425
"psr-4": {

docs/getting-started.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ The job launcher that will be injected depends on the packages you have installe
104104
- if `yokai/batch-symfony-console` is installed, you will receive a `Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher`
105105
- otherwise you will receive a `Yokai\Batch\Launcher\SimpleJobLauncher`
106106

107+
108+
## Use the batchLogger
109+
The aim of the batchLogger is to store log inside the jobExecution.
110+
In a symfony project, you can use the batchLogger with the symfony autowiring by naming your variable as `$yokaiBatchLogger`
111+
112+
```php
113+
<?php
114+
115+
namespace App;
116+
117+
use Psr\Log\LoggerInterface;
118+
119+
final readonly class YourService
120+
{
121+
public function __construct(
122+
private LoggerInterface $yokaiBatchLogger,
123+
) {
124+
}
125+
126+
public function method()
127+
{
128+
$this->yokaiBatchLogger->error(...);
129+
}
130+
}
131+
```
132+
```
133+
107134
## On the same subject
108135

109136
- [What is a job execution storage ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/job-execution-storage.md)

src/DependencyInjection/YokaiBatchExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection;
66

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

7678
private function installed(string $package): bool
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<defaults public="false"/>
9+
10+
<service id="yokai_batch.logger" class="Yokai\Batch\Logger\BatchLogger">
11+
<tag name="kernel.event_listener" method="onPreExecute" event="Yokai\Batch\Event\PreExecuteEvent"/>
12+
<tag name="kernel.event_listener" method="onPostExecute" event="Yokai\Batch\Event\PostExecuteEvent"/>
13+
</service>
14+
15+
</services>
16+
</container>

0 commit comments

Comments
 (0)