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.
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.
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
21 namespace MediaWiki\Logger\Monolog
;
24 use Monolog\Formatter\LineFormatter
as MonologLineFormatter
;
25 use MWExceptionHandler
;
28 * Formats incoming records into a one-line string.
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
36 * @author Bryan Davis <bd808@wikimedia.org>
37 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
39 class LineFormatter
extends MonologLineFormatter
{
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
48 public function __construct(
49 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
50 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
53 $format, $dateFormat, $allowInlineLineBreaks,
54 $ignoreEmptyContextAndExtra
56 $this->includeStacktraces( $includeStacktraces );
61 * Convert an Exception to a string.
66 protected function normalizeException( Exception
$e ) {
67 $str = '[Exception ' . get_class( $e ) . '] (' .
68 $e->getFile() . ':' . $e->getLine() . ') ' .
71 $prev = $e->getPrevious();
73 $str .= ', [Exception ' . get_class( $prev ) . '] (' .
74 $prev->getFile() . ':' . $prev->getLine() . ') ' .
76 $prev = $prev->getPrevious();
79 if ( $this->includeStacktraces
) {
80 $str .= "\n[stacktrace]\n" .
81 MWExceptionHandler
::getRedactedTraceAsString( $e ) .