From e6ed008cea2054535a89be89dc83c455dc6bce71 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 4 Sep 2020 15:01:38 +1200 Subject: [PATCH] Use formatters in QueuedJobHandler 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. --- src/Services/QueuedJobHandler.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Services/QueuedJobHandler.php b/src/Services/QueuedJobHandler.php index 316948e2..2450dc45 100644 --- a/src/Services/QueuedJobHandler.php +++ b/src/Services/QueuedJobHandler.php @@ -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; @@ -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%'); + } }