1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Logger\Monolog;
use Exception;
use Monolog\Formatter\LineFormatter as MonologLineFormatter;
use MWExceptionHandler;
/**
* Formats incoming records into a one-line string.
*
* An 'exeception' in the log record's context will be treated specially.
* It will be output for an '%exception%' placeholder in the format and
* excluded from '%context%' output if the '%exception%' placeholder is
* present.
*
* Exceptions that are logged with this formatter will optional have their
* stack traces appended. If that is done, MWExceptionHandler::redactedTrace()
* will be used to redact the trace information.
*
* @since 1.26
* @author Bryan Davis <bd808@wikimedia.org>
* @copyright © 2015 Bryan Davis and Wikimedia Foundation.
*/
class LineFormatter extends MonologLineFormatter {
/**
* @param string $format The format of the message
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
* @param bool $ignoreEmptyContextAndExtra
* @param bool $includeStacktraces
*/
public function __construct(
$format = null, $dateFormat = null, $allowInlineLineBreaks = false,
$ignoreEmptyContextAndExtra = false, $includeStacktraces = false
) {
parent::__construct(
$format, $dateFormat, $allowInlineLineBreaks,
$ignoreEmptyContextAndExtra
);
$this->includeStacktraces( $includeStacktraces );
}
/**
* {@inheritdoc}
*/
public function format( array $record ) {
// Drop the 'private' flag from the context
unset( $record['context']['private'] );
// Handle exceptions specially: pretty format and remove from context
// Will be output for a '%exception%' placeholder in format
$prettyException = '';
if ( isset( $record['context']['exception'] ) &&
strpos( $this->format, '%exception%' ) !== false
) {
$e = $record['context']['exception'];
unset( $record['context']['exception'] );
if ( $e instanceof Exception ) {
$prettyException = $this->normalizeException( $e );
} elseif ( is_array( $e ) ) {
$prettyException = $this->normalizeExceptionArray( $e );
} else {
$prettyException = $this->stringify( $e );
}
}
$output = parent::format( $record );
if ( strpos( $output, '%exception%' ) !== false ) {
$output = str_replace( '%exception%', $prettyException, $output );
}
return $output;
}
/**
* Convert an Exception to a string.
*
* @param Exception $e
* @return string
*/
protected function normalizeException( Exception $e ) {
return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
}
/**
* Convert an exception to an array of structured data.
*
* @param Exception $e
* @return array
*/
protected function exceptionAsArray( Exception $e ) {
$out = array(
'class' => get_class( $e ),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
);
$prev = $e->getPrevious();
if ( $prev ) {
$out['previous'] = $this->exceptionAsArray( $prev );
}
return $out;
}
/**
* Convert an array of Exception data to a string.
*
* @param array $e
* @return string
*/
protected function normalizeExceptionArray( array $e ) {
$defaults = array(
'class' => 'Unknown',
'file' => 'unknown',
'line' => null,
'message' => 'unknown',
'trace' => array(),
);
$e = array_merge( $defaults, $e );
$str = "\n[Exception {$e['class']}] (" .
"{$e['file']}:{$e['line']}) {$e['message']}";
if ( $this->includeStacktraces && $e['trace'] ) {
$str .= "\n" .
MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
}
if ( isset( $e['previous'] ) ) {
$prev = $e['previous'];
while ( $prev ) {
$prev = array_merge( $defaults, $prev );
$str .= "\nCaused by: [Exception {$prev['class']}] (" .
"{$prev['file']}:{$prev['line']}) {$prev['message']}";
if ( $this->includeStacktraces && $prev['trace'] ) {
$str .= "\n" .
MWExceptionHandler::prettyPrintTrace(
$prev['trace'], ' '
);
}
$prev = isset( $prev['previous'] ) ? $prev['previous'] : null;
}
}
return $str;
}
}
|