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 * 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
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.
40 * @author Bryan Davis <bd808@wikimedia.org>
41 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
43 class LineFormatter
extends MonologLineFormatter
{
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
52 public function __construct(
53 $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
54 $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
57 $format, $dateFormat, $allowInlineLineBreaks,
58 $ignoreEmptyContextAndExtra
60 $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 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 );
99 * Convert an Exception to a string.
101 * @param Exception $e
104 protected function normalizeException( Exception
$e ) {
105 return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
110 * Convert an exception to an array of structured data.
112 * @param Exception $e
115 protected function exceptionAsArray( Exception
$e ) {
117 'class' => get_class( $e ),
118 'message' => $e->getMessage(),
119 'code' => $e->getCode(),
120 'file' => $e->getFile(),
121 'line' => $e->getLine(),
122 'trace' => MWExceptionHandler
::redactTrace( $e->getTrace() ),
125 $prev = $e->getPrevious();
127 $out['previous'] = $this->exceptionAsArray( $prev );
135 * Convert an array of Exception data to a string.
140 protected function normalizeExceptionArray( array $e ) {
142 'class' => 'Unknown',
145 'message' => 'unknown',
148 $e = array_merge( $defaults, $e );
150 $str = "\n[Exception {$e['class']}] (" .
151 "{$e['file']}:{$e['line']}) {$e['message']}";
153 if ( $this->includeStacktraces
&& $e['trace'] ) {
155 MWExceptionHandler
::prettyPrintTrace( $e['trace'], ' ' );
158 if ( isset( $e['previous'] ) ) {
159 $prev = $e['previous'];
161 $prev = array_merge( $defaults, $prev );
162 $str .= "\nCaused by: [Exception {$prev['class']}] (" .
163 "{$prev['file']}:{$prev['line']}) {$prev['message']}";
165 if ( $this->includeStacktraces
&& $prev['trace'] ) {
167 MWExceptionHandler
::prettyPrintTrace(
172 $prev = isset( $prev['previous'] ) ?
$prev['previous'] : null;