MediaWiki REL1_34
LineFormatter.php
Go to the documentation of this file.
1<?php
22
23use Error;
24use Exception;
25use Monolog\Formatter\LineFormatter as MonologLineFormatter;
27use Throwable;
28
44class LineFormatter extends MonologLineFormatter {
45
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
67 public function format( array $record ) {
68 // Drop the 'private' flag from the context
69 unset( $record['context']['private'] );
70
71 // Handle exceptions specially: pretty format and remove from context
72 // Will be output for a '%exception%' placeholder in format
73 $prettyException = '';
74 if ( isset( $record['context']['exception'] ) &&
75 strpos( $this->format, '%exception%' ) !== false
76 ) {
77 $e = $record['context']['exception'];
78 unset( $record['context']['exception'] );
79
80 if ( $e instanceof Throwable || $e instanceof Exception ) {
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 ( strpos( $output, '%exception%' ) !== false ) {
92 $output = str_replace( '%exception%', $prettyException, $output );
93 }
94 return $output;
95 }
96
103 protected function normalizeException( $e ) {
104 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
105 }
106
113 protected function exceptionAsArray( $e ) {
114 $out = [
115 'class' => get_class( $e ),
116 'message' => $e->getMessage(),
117 'code' => $e->getCode(),
118 'file' => $e->getFile(),
119 'line' => $e->getLine(),
120 'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
121 ];
122
123 $prev = $e->getPrevious();
124 if ( $prev ) {
125 $out['previous'] = $this->exceptionAsArray( $prev );
126 }
127
128 return $out;
129 }
130
137 protected function normalizeExceptionArray( array $e ) {
138 $defaults = [
139 'class' => 'Unknown',
140 'file' => 'unknown',
141 'line' => null,
142 'message' => 'unknown',
143 'trace' => [],
144 ];
145 $e = array_merge( $defaults, $e );
146
147 $which = is_a( $e['class'], Error::class, true ) ? 'Error' : 'Exception';
148 $str = "\n[$which {$e['class']}] (" .
149 "{$e['file']}:{$e['line']}) {$e['message']}";
150
151 if ( $this->includeStacktraces && $e['trace'] ) {
152 $str .= "\n" .
153 MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
154 }
155
156 if ( isset( $e['previous'] ) ) {
157 $prev = $e['previous'];
158 while ( $prev ) {
159 $prev = array_merge( $defaults, $prev );
160 $which = is_a( $prev['class'], Error::class, true ) ? 'Error' : 'Exception';
161 $str .= "\nCaused by: [$which {$prev['class']}] (" .
162 "{$prev['file']}:{$prev['line']}) {$prev['message']}";
163
164 if ( $this->includeStacktraces && $prev['trace'] ) {
165 $str .= "\n" .
167 $prev['trace'], ' '
168 );
169 }
170
171 $prev = $prev['previous'] ?? null;
172 }
173 }
174 return $str;
175 }
176}
Handler class for MWExceptions.
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
static redactTrace(array $trace)
Redact a stacktrace generated by Exception::getTrace(), debug_backtrace() or similar means.
Formats incoming records into a one-line string.
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
normalizeException( $e)
Convert a Throwable to a string.
exceptionAsArray( $e)
Convert a throwable to an array of structured data.
normalizeExceptionArray(array $e)
Convert an array of Throwable data to a string.