Lines
100.00%
10 / 10
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StackTraceLineFormatter | 100.00% | 10 / 10 | 100.00% | 3 / 3 | 6 | 100.00% | 1 / 1 | |||
| format | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| dropRepeatedDetails | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| normalizeRecord | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Logging; | |
| 6 | ||
| 7 | use Monolog\Formatter\LineFormatter; | |
| 8 | use Monolog\LogRecord; | |
| 9 | ||
| 10 | final class StackTraceLineFormatter extends LineFormatter | |
| 11 | { | |
| 12 | public function format(LogRecord $record): string | |
| 13 | { | |
| 14 | $trace = $record->extra[ExceptionContextProcessor::TRACE_KEY] ?? null; | |
| 15 | $line = parent::format($this->dropRepeatedDetails($record)); | |
| 16 | ||
| 17 | return \is_string($trace) ? $line . $trace . "\n" : $line; | |
| 18 | } | |
| 19 | ||
| 20 | private function dropRepeatedDetails(LogRecord $record): LogRecord | |
| 21 | { | |
| 22 | $context = $record->context; | |
| 23 | $extra = $record->extra; | |
| 24 | unset($extra[ExceptionContextProcessor::TRACE_KEY]); | |
| 25 | ||
| 26 | // The source tag already names the app; a line about another app keeps its key. | |
| 27 | if (isset($context['app'], $extra[LogOrigin::SOURCE_KEY]) && $context['app'] === $extra[LogOrigin::SOURCE_KEY]) { | |
| 28 | unset($context['app']); | |
| 29 | } | |
| 30 | ||
| 31 | return $record->with(context: $context, extra: $extra); | |
| 32 | } | |
| 33 | ||
| 34 | protected function normalizeRecord(LogRecord $record): array | |
| 35 | { | |
| 36 | return LogOrigin::relabelTracedRecord(parent::normalizeRecord($record)); | |
| 37 | } | |
| 38 | } |