Merge "API: Use message-per-value for apihelp-query+recentchanges-param-prop"
[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 * Exceptions that are logged with this formatter will optional have their
31 * stack traces appended. If that is done,
32 * MWExceptionHandler::getRedactedTraceAsString() will be used to redact the
33 * trace information.
34 *
35 * @since 1.26
36 * @author Bryan Davis <bd808@wikimedia.org>
37 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
38 */
39 class LineFormatter extends MonologLineFormatter {
40
41 /**
42 * @param string $format The format of the message
43 * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
44 * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
45 * @param bool $ignoreEmptyContextAndExtra
46 * @param bool $includeStacktraces
47 */
48 public function __construct(
49 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
50 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
51 ) {
52 parent::__construct(
53 $format, $dateFormat, $allowInlineLineBreaks,
54 $ignoreEmptyContextAndExtra
55 );
56 $this->includeStacktraces( $includeStacktraces );
57 }
58
59
60 /**
61 * Convert an Exception to a string.
62 *
63 * @param Exception $e
64 * @return string
65 */
66 protected function normalizeException( Exception $e ) {
67 $str = '[Exception ' . get_class( $e ) . '] (' .
68 $e->getFile() . ':' . $e->getLine() . ') ' .
69 $e->getMessage();
70
71 $prev = $e->getPrevious();
72 while ( $prev ) {
73 $str .= ', [Exception ' . get_class( $prev ) . '] (' .
74 $prev->getFile() . ':' . $prev->getLine() . ') ' .
75 $prev->getMessage();
76 $prev = $prev->getPrevious();
77 }
78
79 if ( $this->includeStacktraces ) {
80 $str .= "\n[stacktrace]\n" .
81 MWExceptionHandler::getRedactedTraceAsString( $e ) .
82 "\n";
83 }
84
85 return $str;
86 }
87 }