Skip to content

Commit

Permalink
Merge pull request #6 from polderknowledge/feature/human-readable-for…
Browse files Browse the repository at this point in the history
…matter-more-human-readble

The Humand Readable Formatter is now even more human readable.
  • Loading branch information
waltertamboer authored Jun 6, 2018
2 parents 088a04f + 087d0b4 commit cc8377c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 250 deletions.
139 changes: 0 additions & 139 deletions src/Formatter/ExceptionPrinter.php

This file was deleted.

81 changes: 63 additions & 18 deletions src/Formatter/HumanReadableExceptionFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace PolderKnowledge\LogModule\Formatter;

use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\NormalizerFormatter;
use WShafer\PSR11MonoLog\FactoryInterface;

/**
* Format an Exception in a similar way PHP does by default when an exception bubbles to the top
*/
class HumanReadableExceptionFormatter extends NormalizerFormatter implements FormatterInterface, FactoryInterface
class HumanReadableExceptionFormatter extends NormalizerFormatter implements FactoryInterface
{
public function __invoke(array $options)
{
Expand All @@ -22,26 +21,72 @@ public function __invoke(array $options)

public function format(array $record): string
{
$exception = $record['context']['exception'] ?? null;
if ($exception) {
return $this->printFromException($exception);
} else {
return $this->printWithoutException($record);
}
}
$throwable = $record['context']['exception'] ?? null;

protected function printWithoutException(array $record): string
{
return sprintf("[%s] %s: %s\n", ...[
date('r'),
$record = parent::format($record);

$result = sprintf(
"[%s] %s.%s: %s\n\n",
$record['datetime'],
$record['channel'],
$record['level_name'],
$record['message']
]);
);

if (isset($record['context']['file'])) {
$result .= "[Context]\n\n";

if (isset($record['context']['message'])) {
$result .= sprintf(" Message: %s\n", $record['context']['message']);
}

$result .= sprintf(" File: %s\n", $record['context']['file']);
$result .= sprintf(" Line: %d\n", $record['context']['line']);

if (isset($record['context']['code'])) {
$result .= sprintf(" Code: %s\n\n", $record['context']['code']);
}
}

$exceptionCounter = 1;

while ($throwable !== null) {
$result .= sprintf("[Exception #%d]\n", $exceptionCounter++);
$result .= sprintf(" Type: %s\n", get_class($throwable));
$result .= sprintf(" Message: %s\n", $throwable->getMessage());
$result .= sprintf(" Code: %d\n", $throwable->getCode());
$result .= sprintf(" File: %s\n", $throwable->getFile());
$result .= sprintf(" Line: %d\n\n", $throwable->getLine());

$result .= "[Trace]\n";
$result .= $this->indentLines($throwable->getTraceAsString());
$result .= "\n\n";

$throwable = $throwable->getPrevious();
}

foreach ($record['extra'] as $key => $params) {
if (is_array($params) || is_object($params)) {
$lines = json_encode($params, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
} else {
$lines = $params;
}

$result .= "[Extra - " . $key . "]\n";
$result .= $this->indentLines($lines) . "\n\n";
}

return $result;
}
protected function printFromException(\Throwable $exception)

private function indentLines(string $input, int $spaces = 2)
{
return implode("\n", ExceptionPrinter::linesFromException($exception)) . "\n"
. "---------------------------------------\n";
$lines = explode("\n", $input);

$indented = array_map(function ($item) use ($spaces) {
return str_repeat(' ', $spaces) . $item;
}, $lines);

return implode("\n", $indented);
}
}
38 changes: 0 additions & 38 deletions tests/Formatter/FormatArgumentTest.php

This file was deleted.

55 changes: 0 additions & 55 deletions tests/Formatter/FormatTraceTest.php

This file was deleted.

0 comments on commit cc8377c

Please sign in to comment.