Skip to content

Commit 9d4ac48

Browse files
committed
#88 add helper method to log error without adding failure
1 parent f6f6826 commit 9d4ac48

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/batch/src/JobExecution.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DateTime;
99
use DateTimeInterface;
1010
use Psr\Log\LoggerInterface;
11+
use Psr\Log\LogLevel;
1112
use Throwable;
1213
use Yokai\Batch\Exception\ImmutablePropertyException;
1314
use Yokai\Batch\Factory\JobExecutionIdGeneratorInterface;
@@ -379,4 +380,15 @@ public function getLogger(): LoggerInterface
379380
{
380381
return $this->logger;
381382
}
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+
}
382394
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)