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
;
25 use Monolog\Formatter\LineFormatter
as MonologLineFormatter
;
26 use MWExceptionHandler
;
30 * Formats incoming records into a one-line string.
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
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.
42 * @copyright © 2015 Wikimedia Foundation and contributors
44 class LineFormatter
extends MonologLineFormatter
{
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
53 public function __construct(
54 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
55 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
58 $format, $dateFormat, $allowInlineLineBreaks,
59 $ignoreEmptyContextAndExtra
61 $this->includeStacktraces( $includeStacktraces );
67 public function format( array $record ) {
68 // Drop the 'private' flag from the context
69 unset( $record['context']['private'] );
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
77 $e = $record['context']['exception'];
78 unset( $record['context']['exception'] );
80 if ( $e instanceof Throwable ||
$e instanceof Exception
) {
81 $prettyException = $this->normalizeException( $e );
82 } elseif ( is_array( $e ) ) {
83 $prettyException = $this->normalizeExceptionArray( $e );
85 $prettyException = $this->stringify( $e );
89 $output = parent
::format( $record );
91 if ( strpos( $output, '%exception%' ) !== false ) {
92 $output = str_replace( '%exception%', $prettyException, $output );
98 * Convert a Throwable to a string.
100 * @param Exception|Throwable $e
103 protected function normalizeException( $e ) {
104 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
108 * Convert a throwable to an array of structured data.
110 * @param Exception|Throwable $e
113 protected function exceptionAsArray( $e ) {
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() ),
123 $prev = $e->getPrevious();
125 $out['previous'] = $this->exceptionAsArray( $prev );
132 * Convert an array of Throwable data to a string.
137 protected function normalizeExceptionArray( array $e ) {
139 'class' => 'Unknown',
142 'message' => 'unknown',
145 $e = array_merge( $defaults, $e );
147 $which = is_a( $e['class'], Error
::class, true ) ?
'Error' : 'Exception';
148 $str = "\n[$which {$e['class']}] (" .
149 "{$e['file']}:{$e['line']}) {$e['message']}";
151 if ( $this->includeStacktraces
&& $e['trace'] ) {
153 MWExceptionHandler
::prettyPrintTrace( $e['trace'], ' ' );
156 if ( isset( $e['previous'] ) ) {
157 $prev = $e['previous'];
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']}";
164 if ( $this->includeStacktraces
&& $prev['trace'] ) {
166 MWExceptionHandler
::prettyPrintTrace(
171 $prev = $prev['previous'] ??
null;