MediaWiki REL1_31
LineFormatter.php
Go to the documentation of this file.
1<?php
22
23use Exception;
24use Monolog\Formatter\LineFormatter as MonologLineFormatter;
26
42class LineFormatter extends MonologLineFormatter {
43
51 public function __construct(
52 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
53 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
54 ) {
55 parent::__construct(
56 $format, $dateFormat, $allowInlineLineBreaks,
57 $ignoreEmptyContextAndExtra
58 );
59 $this->includeStacktraces( $includeStacktraces );
60 }
61
65 public function format( array $record ) {
66 // Drop the 'private' flag from the context
67 unset( $record['context']['private'] );
68
69 // Handle exceptions specially: pretty format and remove from context
70 // Will be output for a '%exception%' placeholder in format
71 $prettyException = '';
72 if ( isset( $record['context']['exception'] ) &&
73 strpos( $this->format, '%exception%' ) !== false
74 ) {
75 $e = $record['context']['exception'];
76 unset( $record['context']['exception'] );
77
78 if ( $e instanceof Exception ) {
79 $prettyException = $this->normalizeException( $e );
80 } elseif ( is_array( $e ) ) {
81 $prettyException = $this->normalizeExceptionArray( $e );
82 } else {
83 $prettyException = $this->stringify( $e );
84 }
85 }
86
87 $output = parent::format( $record );
88
89 if ( strpos( $output, '%exception%' ) !== false ) {
90 $output = str_replace( '%exception%', $prettyException, $output );
91 }
92 return $output;
93 }
94
101 protected function normalizeException( $e ) {
102 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
103 }
104
111 protected function exceptionAsArray( Exception $e ) {
112 $out = [
113 'class' => get_class( $e ),
114 'message' => $e->getMessage(),
115 'code' => $e->getCode(),
116 'file' => $e->getFile(),
117 'line' => $e->getLine(),
118 'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
119 ];
120
121 $prev = $e->getPrevious();
122 if ( $prev ) {
123 $out['previous'] = $this->exceptionAsArray( $prev );
124 }
125
126 return $out;
127 }
128
135 protected function normalizeExceptionArray( array $e ) {
136 $defaults = [
137 'class' => 'Unknown',
138 'file' => 'unknown',
139 'line' => null,
140 'message' => 'unknown',
141 'trace' => [],
142 ];
143 $e = array_merge( $defaults, $e );
144
145 $str = "\n[Exception {$e['class']}] (" .
146 "{$e['file']}:{$e['line']}) {$e['message']}";
147
148 if ( $this->includeStacktraces && $e['trace'] ) {
149 $str .= "\n" .
151 }
152
153 if ( isset( $e['previous'] ) ) {
154 $prev = $e['previous'];
155 while ( $prev ) {
156 $prev = array_merge( $defaults, $prev );
157 $str .= "\nCaused by: [Exception {$prev['class']}] (" .
158 "{$prev['file']}:{$prev['line']}) {$prev['message']}";
159
160 if ( $this->includeStacktraces && $prev['trace'] ) {
161 $str .= "\n" .
163 $prev['trace'], ' '
164 );
165 }
166
167 $prev = isset( $prev['previous'] ) ? $prev['previous'] : null;
168 }
169 }
170 return $str;
171 }
172}
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.
exceptionAsArray(Exception $e)
Convert an exception to an array of structured data.
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
normalizeException( $e)
Convert an Exception to a string.
normalizeExceptionArray(array $e)
Convert an array of Exception data to a string.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2255
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
returning false will NOT prevent logging $e
Definition hooks.txt:2176