Skip to content

Commit

Permalink
Use formatters in QueuedJobHandler
Browse files Browse the repository at this point in the history
If you check other handlers, they all call format() as part of their handle() implementation.
But this handler is wrapped in a BufferHandler, and that's expecting handleBatch() implementations instead.
So unlike other handlers, this one doesn't inherit the default formatter invocation behaviour from parent implementations.
This wasn't a problem before because the formatting was just inlined into the custom code (QueuedJobService),
which is an anti-pattern in terms of Monolog usage. Compare this to SyslogHandler and AbstractSyslogHandler.
  • Loading branch information
chillu committed Sep 18, 2020
1 parent f83b8b4 commit b78f296
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Services/QueuedJobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Symbiote\QueuedJobs\Services;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use SilverStripe\Core\Injector\Injectable;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
Expand Down Expand Up @@ -56,11 +57,21 @@ protected function write(array $record)

public function handleBatch(array $records)
{
foreach ($records as $record) {
$this->job->addMessage($record['message'], $record['level_name'], $record['datetime']);
foreach ($records as $i => $record) {
$records[$i] = $this->processRecord($records[$i]);
$records[$i]['formatted'] = $this->getFormatter()->format($records[$i]);
$this->job->addMessage($records[$i]['formatted'], $records[$i]['level_name'], $records[$i]['datetime']);
};
$this->jobDescriptor->SavedJobMessages = serialize($this->job->getJobData()->messages);

$this->jobDescriptor->write();
}

/**
* Ensure that exception context is retained. Similar logic to SyslogHandler.
*/
protected function getDefaultFormatter()
{
return new LineFormatter('%message% %context% %extra%');
}
}

0 comments on commit b78f296

Please sign in to comment.