User: Avoid deprecated Linker::link()
[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 Exception;
24 use Monolog\Formatter\LineFormatter as MonologLineFormatter;
25 use MWExceptionHandler;
26
27 /**
28 * Formats incoming records into a one-line string.
29 *
30 * An 'exeception' in the log record's context will be treated specially.
31 * It will be output for an '%exception%' placeholder in the format and
32 * excluded from '%context%' output if the '%exception%' placeholder is
33 * present.
34 *
35 * Exceptions that are logged with this formatter will optional have their
36 * stack traces appended. If that is done, MWExceptionHandler::redactedTrace()
37 * will be used to redact the trace information.
38 *
39 * @since 1.26
40 * @author Bryan Davis <bd808@wikimedia.org>
41 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
42 */
43 class LineFormatter extends MonologLineFormatter {
44
45 /**
46 * @param string $format The format of the message
47 * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
48 * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
49 * @param bool $ignoreEmptyContextAndExtra
50 * @param bool $includeStacktraces
51 */
52 public function __construct(
53 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
54 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
55 ) {
56 parent::__construct(
57 $format, $dateFormat, $allowInlineLineBreaks,
58 $ignoreEmptyContextAndExtra
59 );
60 $this->includeStacktraces( $includeStacktraces );
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 public function format( array $record ) {
67 // Drop the 'private' flag from the context
68 unset( $record['context']['private'] );
69
70 // Handle exceptions specially: pretty format and remove from context
71 // Will be output for a '%exception%' placeholder in format
72 $prettyException = '';
73 if ( isset( $record['context']['exception'] ) &&
74 strpos( $this->format, '%exception%' ) !== false
75 ) {
76 $e = $record['context']['exception'];
77 unset( $record['context']['exception'] );
78
79 if ( $e instanceof Exception ) {
80 $prettyException = $this->normalizeException( $e );
81 } elseif ( is_array( $e ) ) {
82 $prettyException = $this->normalizeExceptionArray( $e );
83 } else {
84 $prettyException = $this->stringify( $e );
85 }
86 }
87
88 $output = parent::format( $record );
89
90 if ( strpos( $output, '%exception%' ) !== false ) {
91 $output = str_replace( '%exception%', $prettyException, $output );
92 }
93 return $output;
94 }
95
96 /**
97 * Convert an Exception to a string.
98 *
99 * @param Exception $e
100 * @return string
101 */
102 protected function normalizeException( $e ) {
103 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
104 }
105
106 /**
107 * Convert an exception to an array of structured data.
108 *
109 * @param Exception $e
110 * @return array
111 */
112 protected function exceptionAsArray( Exception $e ) {
113 $out = [
114 'class' => get_class( $e ),
115 'message' => $e->getMessage(),
116 'code' => $e->getCode(),
117 'file' => $e->getFile(),
118 'line' => $e->getLine(),
119 'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
120 ];
121
122 $prev = $e->getPrevious();
123 if ( $prev ) {
124 $out['previous'] = $this->exceptionAsArray( $prev );
125 }
126
127 return $out;
128 }
129
130 /**
131 * Convert an array of Exception data to a string.
132 *
133 * @param array $e
134 * @return string
135 */
136 protected function normalizeExceptionArray( array $e ) {
137 $defaults = [
138 'class' => 'Unknown',
139 'file' => 'unknown',
140 'line' => null,
141 'message' => 'unknown',
142 'trace' => [],
143 ];
144 $e = array_merge( $defaults, $e );
145
146 $str = "\n[Exception {$e['class']}] (" .
147 "{$e['file']}:{$e['line']}) {$e['message']}";
148
149 if ( $this->includeStacktraces && $e['trace'] ) {
150 $str .= "\n" .
151 MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
152 }
153
154 if ( isset( $e['previous'] ) ) {
155 $prev = $e['previous'];
156 while ( $prev ) {
157 $prev = array_merge( $defaults, $prev );
158 $str .= "\nCaused by: [Exception {$prev['class']}] (" .
159 "{$prev['file']}:{$prev['line']}) {$prev['message']}";
160
161 if ( $this->includeStacktraces && $prev['trace'] ) {
162 $str .= "\n" .
163 MWExceptionHandler::prettyPrintTrace(
164 $prev['trace'], ' '
165 );
166 }
167
168 $prev = isset( $prev['previous'] ) ? $prev['previous'] : null;
169 }
170 }
171 return $str;
172 }
173 }