Skip to content
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

Format exception log for Elasticsearch #1393

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Monolog/Formatter/ElasticsearchFormatter.php
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
);
}
}
2 changes: 1 addition & 1 deletion tests/Monolog/Handler/ProcessHandlerTest.php
Expand Up @@ -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]));
Expand Down