-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add job error logging #309
Open
shoosah
wants to merge
2
commits into
symbiote:4
Choose a base branch
from
silverstripe-terraformers:feature/job-error-logging
base: 4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Symbiote\QueuedJobs\Util; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
use Symbiote\QueuedJobs\Services\QueuedJob; | ||
|
||
/** | ||
* Class Logger | ||
* | ||
* This logger redirects all log messages to the queued job | ||
* which makes the job data contain all relevant logs | ||
* | ||
* @package Symbiote\QueuedJobs\Util | ||
*/ | ||
class Logger implements LoggerInterface | ||
{ | ||
/** | ||
* @var QueuedJob|null | ||
*/ | ||
private $job = null; | ||
|
||
public function setJob(?QueuedJob $job): self | ||
{ | ||
$this->job = $job; | ||
return $this; | ||
} | ||
|
||
public function getJob(): ?QueuedJob | ||
{ | ||
return $this->job; | ||
} | ||
|
||
public function debug($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::DEBUG); | ||
} | ||
|
||
public function critical($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::CRITICAL); | ||
} | ||
|
||
public function alert($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::ALERT); | ||
} | ||
|
||
public function emergency($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::EMERGENCY); | ||
} | ||
|
||
public function warning($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::WARNING); | ||
} | ||
|
||
public function error($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::ERROR); | ||
} | ||
|
||
public function notice($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::NOTICE); | ||
} | ||
|
||
public function info($message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, LogLevel::INFO); | ||
} | ||
|
||
public function log($level, $message, array $context = []): void | ||
{ | ||
$this->logJobMessage($message, $level, $context); | ||
} | ||
|
||
private function logJobMessage(string $message, string $level, array $context = []): void | ||
{ | ||
$job = $this->job; | ||
|
||
if (!$job instanceof QueuedJob) { | ||
return; | ||
} | ||
|
||
$job->addMessage($message, $level); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
namespace Symbiote\QueuedJobs\Util; | ||
|
||
use Psr\Log\LogLevel; | ||
use Symbiote\QueuedJobs\Tests\AbstractTest; | ||
use Symbiote\QueuedJobs\Tests\QueuedJobsTest\TestQJService; | ||
use Symbiote\QueuedJobs\Tests\QueuedJobsTest\TestQueuedJob; | ||
|
||
class LoggerTest extends AbstractTest | ||
{ | ||
/** | ||
* We need the DB for this test | ||
* | ||
* @var bool | ||
*/ | ||
protected $usesDatabase = true; | ||
|
||
/** | ||
* @return TestQJService | ||
*/ | ||
protected function getService() | ||
{ | ||
return singleton(TestQJService::class); | ||
} | ||
|
||
private function getLogger() | ||
{ | ||
$service = $this->getService(); | ||
|
||
// Create a job and add it to the queue | ||
$job = new TestQueuedJob(); | ||
$service->queueJob($job); | ||
|
||
// Create a logger and set it for the created job | ||
$logger = new Logger(); | ||
return $logger->setJob($job); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testSetLogger() | ||
{ | ||
$logger = $this->getLogger(); | ||
$this->assertNotNull($logger); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testDebug() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is debug message'; | ||
$logger->debug($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::DEBUG), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testCritical() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is critical message'; | ||
$logger->critical($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::CRITICAL), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testAlert() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is alert message'; | ||
$logger->alert($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::ALERT), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testEmergency() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is emergency message'; | ||
$logger->emergency($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::EMERGENCY), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testWarning() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is warning message'; | ||
$logger->warning($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::WARNING), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testError() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is error message'; | ||
$logger->error($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::ERROR), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testNotice() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is notice message'; | ||
$logger->notice($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::NOTICE), $jobData); | ||
} | ||
|
||
/** | ||
* @group logger | ||
*/ | ||
public function testInfo() | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is info message'; | ||
$logger->info($message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper(LogLevel::INFO), $jobData); | ||
} | ||
|
||
/** | ||
* @dataProvider loggerProvider | ||
* @param $logLevel | ||
* @group logger | ||
*/ | ||
public function testLog($logLevel) | ||
{ | ||
$logger = $this->getLogger(); | ||
|
||
$message = 'This is info message'; | ||
$logger->log($logLevel, $message); | ||
|
||
$jobData = $logger->getJob()->getJobData()->messages[0]; | ||
$this->assertContains($message, $jobData); | ||
$this->assertContains(strtoupper($logLevel), $jobData); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function loggerProvider() | ||
{ | ||
return [ | ||
[LogLevel::WARNING], | ||
[LogLevel::EMERGENCY], | ||
[LogLevel::ALERT], | ||
[LogLevel::CRITICAL], | ||
[LogLevel::ERROR], | ||
[LogLevel::NOTICE], | ||
[LogLevel::INFO], | ||
[LogLevel::DEBUG], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this example should be changed to reflect a more common situation where the logging is attached within the job execution. i.e. within the
process()
function of the jobThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that
Helper
isn't defined, I still think this is quite confusing. Regardless, I'm not sure why there's a need to mix in the concept of a "helper class" with the creation of a logger, seems like unnecessary noise? It's pretty clear how a logger can be used (incl. passing it to other hypothetical dependencies) once it's created.