File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 8
8
use DateTime ;
9
9
use DateTimeInterface ;
10
10
use Psr \Log \LoggerInterface ;
11
+ use Psr \Log \LogLevel ;
11
12
use Throwable ;
12
13
use Yokai \Batch \Exception \ImmutablePropertyException ;
13
14
use Yokai \Batch \Factory \JobExecutionIdGeneratorInterface ;
@@ -379,4 +380,15 @@ public function getLogger(): LoggerInterface
379
380
{
380
381
return $ this ->logger ;
381
382
}
383
+
384
+ /**
385
+ * Log the error from a Throwable
386
+ * @param Throwable $error The error to log
387
+ * @param string|null $message The message to use while logging
388
+ * @param LogLevel::* $level The level to use while logging
389
+ */
390
+ public function logError (Throwable $ error , string $ message = null , string $ level = LogLevel::ERROR ): void
391
+ {
392
+ $ this ->logger ->log ($ level , $ message ?? 'An error occurred ' , ['error ' => (string )$ error ]);
393
+ }
382
394
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Tucania project.
5
+ *
6
+ * Copyright (C) Tucania (https://www.tucania.com/) - All Rights Reserved
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
9
+ */
10
+
11
+ declare (strict_types=1 );
12
+
13
+ namespace Yokai \Batch \Sources \Tests \Convention \Job ;
14
+
15
+ use Exception ;
16
+ use PHPUnit \Framework \TestCase ;
17
+ use Yokai \Batch \Factory \JobExecutionFactory ;
18
+ use Yokai \Batch \Factory \UniqidJobExecutionIdGenerator ;
19
+
20
+ class LoggerTest extends TestCase
21
+ {
22
+ private JobExecutionFactory $ jobExecutionFactory ;
23
+
24
+ public function setUp (): void
25
+ {
26
+ $ idGenerator = new UniqidJobExecutionIdGenerator ();
27
+ $ this ->jobExecutionFactory = new JobExecutionFactory ($ idGenerator );
28
+ }
29
+
30
+ public function testLogError ()
31
+ {
32
+ $ errorToLog = 'test assert logErrorMethod ' ;
33
+ $ jobExecution = $ this ->jobExecutionFactory ->create ('job-test ' );
34
+ $ jobExecution ->logError (new Exception (), $ errorToLog );
35
+
36
+ self ::assertStringContainsString ($ errorToLog , current (((array )$ jobExecution ->getLogs ())));
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments