Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / debug / logger / monolog / LineFormatter.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger\Monolog;
22
23 use Error;
24 use Exception;
25 use Monolog\Formatter\LineFormatter as MonologLineFormatter;
26 use MWExceptionHandler;
27 use Throwable;
28
29 /**
30 * Formats incoming records into a one-line string.
31 *
32 * An 'exeception' in the log record's context will be treated specially.
33 * It will be output for an '%exception%' placeholder in the format and
34 * excluded from '%context%' output if the '%exception%' placeholder is
35 * present.
36 *
37 * Throwables that are logged with this formatter will optional have their
38 * stack traces appended. If that is done, MWExceptionHandler::redactedTrace()
39 * will be used to redact the trace information.
40 *
41 * @since 1.26
42 * @copyright © 2015 Wikimedia Foundation and contributors
43 */
44 class LineFormatter extends MonologLineFormatter {
45
46 /**
47 * @param string|null $format The format of the message
48 * @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
49 * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
50 * @param bool $ignoreEmptyContextAndExtra
51 * @param bool $includeStacktraces
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 * @inheritDoc
66 */
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
97 /**
98 * Convert a Throwable to a string.
99 *
100 * @param Exception|Throwable $e
101 * @return string
102 */
103 protected function normalizeException( $e ) {
104 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
105 }
106
107 /**
108 * Convert a throwable to an array of structured data.
109 *
110 * @param Exception|Throwable $e
111 * @return array
112 */
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
131 /**
132 * Convert an array of Throwable data to a string.
133 *
134 * @param array $e
135 * @return string
136 */
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" .
166 MWExceptionHandler::prettyPrintTrace(
167 $prev['trace'], ' '
168 );
169 }
170
171 $prev = $prev['previous'] ?? null;
172 }
173 }
174 return $str;
175 }
176 }