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
12 changes: 12 additions & 0 deletions src/batch/src/JobExecution.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTime;
use DateTimeInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Throwable;
use Yokai\Batch\Exception\ImmutablePropertyException;
use Yokai\Batch\Factory\JobExecutionIdGeneratorInterface;
Expand Down Expand Up @@ -379,4 +380,15 @@ public function getLogger(): LoggerInterface
{
return $this->logger;
}

/**
* Log the error from a Throwable
* @param Throwable $error The error to log
* @param string|null $message The message to use while logging
* @param LogLevel::* $level The level to use while logging
*/
public function logError(Throwable $error, string $message = null, string $level = LogLevel::ERROR): void
{
$this->logger->log($level, $message ?? 'An error occurred', ['error' => (string)$error]);
}
}
25 changes: 25 additions & 0 deletions src/batch/tests/Job/LoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Job;

use Exception;
use PHPUnit\Framework\TestCase;
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\JobExecution;

class LoggerTest extends TestCase
{
public function testLogError()
{
$idGenerator = new UniqidJobExecutionIdGenerator();
$errorToLog = 'test assert logErrorMethod';
$errorException = 'test assert errorException';
$jobExecution = JobExecution::createRoot($idGenerator->generate(), 'export');
$jobExecution->logError(new Exception($errorException), $errorToLog);

self::assertStringContainsString($errorToLog, $jobExecution->getLogs()->__toString());
self::assertStringContainsString($errorException, $jobExecution->getLogs()->__toString());
}
}