| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Logger\Monolog; |
| 22 |
|
| 23 |
use Error; |
| 24 |
use Monolog\Formatter\LineFormatter as MonologLineFormatter; |
| 25 |
use MWExceptionHandler; |
| 26 |
use Throwable; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
s |
| 43 |
|
| 44 |
class LineFormatter extends MonologLineFormatter { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( |
| 54 |
$format = null, $dateFormat = null, $allowInlineLineBreaks = false, |
| 55 |
$ignoreEmptyContextAndExtra = false, $includeStacktraces = false |
| 56 |
) { |
| 57 |
parent::__construct( |
| 58 |
$format, $dateFormat, $allowInlineLineBreaks, |
| 59 |
$ignoreEmptyContextAndExtra |
| 60 |
); |
| 61 |
$this->includeStacktraces( $includeStacktraces ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function format( array $record ): string { |
| 68 |
|
| 69 |
unset( $record['context']['private'] ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
$prettyException = ''; |
| 74 |
if ( isset( $record['context']['exception'] ) && |
| 75 |
str_contains( $this->format, '%exception%' ) |
| 76 |
) { |
| 77 |
$e = $record['context']['exception']; |
| 78 |
unset( $record['context']['exception'] ); |
| 79 |
|
| 80 |
if ( $e instanceof Throwable ) { |
| 81 |
$prettyException = $this->normalizeException( $e ); |
| 82 |
} elseif ( is_array( $e ) ) { |
| 83 |
$prettyException = $this->normalizeExceptionArray( $e ); |
| 84 |
} else { |
| 85 |
$prettyException = $this->stringify( $e ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$output = parent::format( $record ); |
| 90 |
|
| 91 |
if ( str_contains( $output, '%exception%' ) ) { |
| 92 |
$output = str_replace( '%exception%', $prettyException, $output ); |
| 93 |
} |
| 94 |
return $output; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
protected function normalizeException( Throwable $e, int $depth = 0 ): string { |
| 105 |
|
| 106 |
return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected function exceptionAsArray( Throwable $e ) { |
| 116 |
$out = [ |
| 117 |
'class' => get_class( $e ), |
| 118 |
'message' => $e->getMessage(), |
| 119 |
'code' => $e->getCode(), |
| 120 |
'file' => $e->getFile(), |
| 121 |
'line' => $e->getLine(), |
| 122 |
'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ), |
| 123 |
]; |
| 124 |
|
| 125 |
$prev = $e->getPrevious(); |
| 126 |
if ( $prev ) { |
| 127 |
$out['previous'] = $this->exceptionAsArray( $prev ); |
| 128 |
} |
| 129 |
|
| 130 |
return $out; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected function normalizeExceptionArray( array $e ) { |
| 140 |
$defaults = [ |
| 141 |
'class' => 'Unknown', |
| 142 |
'file' => 'unknown', |
| 143 |
'line' => null, |
| 144 |
'message' => 'unknown', |
| 145 |
'trace' => [], |
| 146 |
]; |
| 147 |
$e = array_merge( $defaults, $e ); |
| 148 |
|
| 149 |
|
| 150 |
$which = is_a( $e['class'], Error::class, true ) ? 'Error' : 'Exception'; |
| 151 |
$str = "\n[$which {$e['class']}] (" . |
| 152 |
"{$e['file']}:{$e['line']}) {$e['message']}"; |
| 153 |
|
| 154 |
if ( $this->includeStacktraces && $e['trace'] ) { |
| 155 |
$str .= "\n" . |
| 156 |
|
| 157 |
MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' ); |
| 158 |
} |
| 159 |
|
| 160 |
if ( isset( $e['previous'] ) ) { |
| 161 |
$prev = $e['previous']; |
| 162 |
while ( $prev ) { |
| 163 |
$prev = array_merge( $defaults, $prev ); |
| 164 |
$which = is_a( $prev['class'], Error::class, true ) ? 'Error' : 'Exception'; |
| 165 |
$str .= "\nCaused by: [$which {$prev['class']}] (" . |
| 166 |
"{$prev['file']}:{$prev['line']}) {$prev['message']}"; |
| 167 |
|
| 168 |
if ( $this->includeStacktraces && $prev['trace'] ) { |
| 169 |
$str .= "\n" . |
| 170 |
MWExceptionHandler::prettyPrintTrace( |
| 171 |
$prev['trace'], ' ' |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
$prev = $prev['previous'] ?? null; |
| 176 |
} |
| 177 |
} |
| 178 |
return $str; |
| 179 |
} |
| 180 |
} |