From c81c468373393be066141d5f167181bb762e453f Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Wed, 30 Oct 2019 13:55:49 +0700 Subject: [PATCH 1/3] Format exception log for Elasticsearch --- .../Formatter/ElasticsearchFormatter.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/Monolog/Formatter/ElasticsearchFormatter.php b/src/Monolog/Formatter/ElasticsearchFormatter.php index 84affef35..6f8838243 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($context): bool + { + return ( + is_array($context) + && array_key_exists('exception', $context) + ); + } } From 3ef4998ca79509878c73da0301bfbe767e6887e5 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Wed, 30 Oct 2019 14:23:59 +0700 Subject: [PATCH 2/3] Format exception log for Elasticsearch --- src/Monolog/Formatter/ElasticsearchFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/ElasticsearchFormatter.php b/src/Monolog/Formatter/ElasticsearchFormatter.php index 6f8838243..fcc01da13 100644 --- a/src/Monolog/Formatter/ElasticsearchFormatter.php +++ b/src/Monolog/Formatter/ElasticsearchFormatter.php @@ -130,7 +130,7 @@ protected function recordHasContext(array $record): bool * * @return bool */ - protected function contextHasException($context): bool + protected function contextHasException(array $context): bool { return ( is_array($context) From b253d4dd67bd338ebd6c0df6a5aa2c83bf0ea76c Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Wed, 30 Oct 2019 16:18:53 +0700 Subject: [PATCH 3/3] Update test handler --- tests/Monolog/Handler/ProcessHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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]));