diff --git a/src/Monolog/Formatter/ElasticsearchFormatter.php b/src/Monolog/Formatter/ElasticsearchFormatter.php index 84affef35..fcc01da13 100644 --- a/src/Monolog/Formatter/ElasticsearchFormatter.php +++ b/src/Monolog/Formatter/ElasticsearchFormatter.php @@ -50,6 +50,10 @@ public function format(array $record) { $record = parent::format($record); + if ($this->recordHasContext($record) && $this->contextHasException($record['context'])) { + $record['context']['exception'] = $this->getContextException($record['context']['exception']); + } + return $this->getDocument($record); } @@ -86,4 +90,51 @@ protected function getDocument(array $record): array return $record; } + + /** + * Returns the entire exception as Elasticsearch format + * + * @param array $recordContext + * + * @return array + */ + protected function getContextException(array $recordContext) + { + return [ + 'class' => $recordContext['class'] ?? '', + 'message' => $recordContext['message'] ?? '', + 'code' => intval($recordContext['code']) ?? '', + 'file' => $recordContext['file'] ?? '', + 'trace' => $recordContext['trace'] ?? '', + ]; + } + + /** + * Identifies the content type of the given $record + * + * @param array $record + * + * @return bool + */ + protected function recordHasContext(array $record): bool + { + return ( + array_key_exists('context', $record) + ); + } + + /** + * Identifies the content type of the given $context + * + * @param mixed $context + * + * @return bool + */ + protected function contextHasException(array $context): bool + { + return ( + is_array($context) + && array_key_exists('exception', $context) + ); + } } diff --git a/tests/Monolog/Handler/ProcessHandlerTest.php b/tests/Monolog/Handler/ProcessHandlerTest.php index fc1aff475..70619e700 100644 --- a/tests/Monolog/Handler/ProcessHandlerTest.php +++ b/tests/Monolog/Handler/ProcessHandlerTest.php @@ -48,7 +48,7 @@ public function testWriteOpensProcessAndWritesToStdInOfProcess() $handler->expects($this->exactly(2)) ->method('writeProcessInput') - ->withConsecutive($this->stringContains($fixtures[0]), $this->stringContains($fixtures[1])); + ->withConsecutive([$this->stringContains($fixtures[0])], [$this->stringContains($fixtures[1])]); /** @var ProcessHandler $handler */ $handler->handle($this->getRecord(Logger::WARNING, $fixtures[0]));