MediaWiki  1.34.0
LineFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
23 use Error;
24 use Exception;
25 use Monolog\Formatter\LineFormatter as MonologLineFormatter;
27 use Throwable;
28 
44 class LineFormatter extends MonologLineFormatter {
45 
53  public function __construct(
54  $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
55  $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
56  ) {
57  parent::__construct(
58  $format, $dateFormat, $allowInlineLineBreaks,
59  $ignoreEmptyContextAndExtra
60  );
61  $this->includeStacktraces( $includeStacktraces );
62  }
63 
67  public function format( array $record ) {
68  // Drop the 'private' flag from the context
69  unset( $record['context']['private'] );
70 
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
76  ) {
77  $e = $record['context']['exception'];
78  unset( $record['context']['exception'] );
79 
80  if ( $e instanceof Throwable || $e instanceof Exception ) {
81  $prettyException = $this->normalizeException( $e );
82  } elseif ( is_array( $e ) ) {
83  $prettyException = $this->normalizeExceptionArray( $e );
84  } else {
85  $prettyException = $this->stringify( $e );
86  }
87  }
88 
89  $output = parent::format( $record );
90 
91  if ( strpos( $output, '%exception%' ) !== false ) {
92  $output = str_replace( '%exception%', $prettyException, $output );
93  }
94  return $output;
95  }
96 
103  protected function normalizeException( $e ) {
104  return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
105  }
106 
113  protected function exceptionAsArray( $e ) {
114  $out = [
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() ),
121  ];
122 
123  $prev = $e->getPrevious();
124  if ( $prev ) {
125  $out['previous'] = $this->exceptionAsArray( $prev );
126  }
127 
128  return $out;
129  }
130 
137  protected function normalizeExceptionArray( array $e ) {
138  $defaults = [
139  'class' => 'Unknown',
140  'file' => 'unknown',
141  'line' => null,
142  'message' => 'unknown',
143  'trace' => [],
144  ];
145  $e = array_merge( $defaults, $e );
146 
147  $which = is_a( $e['class'], Error::class, true ) ? 'Error' : 'Exception';
148  $str = "\n[$which {$e['class']}] (" .
149  "{$e['file']}:{$e['line']}) {$e['message']}";
150 
151  if ( $this->includeStacktraces && $e['trace'] ) {
152  $str .= "\n" .
153  MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
154  }
155 
156  if ( isset( $e['previous'] ) ) {
157  $prev = $e['previous'];
158  while ( $prev ) {
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']}";
163 
164  if ( $this->includeStacktraces && $prev['trace'] ) {
165  $str .= "\n" .
167  $prev['trace'], ' '
168  );
169  }
170 
171  $prev = $prev['previous'] ?? null;
172  }
173  }
174  return $str;
175  }
176 }
MWExceptionHandler
Handler class for MWExceptions.
Definition: MWExceptionHandler.php:30
MediaWiki\Logger\Monolog\LineFormatter\exceptionAsArray
exceptionAsArray( $e)
Convert a throwable to an array of structured data.
Definition: LineFormatter.php:113
MWExceptionHandler\redactTrace
static redactTrace(array $trace)
Redact a stacktrace generated by Exception::getTrace(), debug_backtrace() or similar means.
Definition: MWExceptionHandler.php:477
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
MediaWiki\Logger\Monolog\LineFormatter\normalizeException
normalizeException( $e)
Convert a Throwable to a string.
Definition: LineFormatter.php:103
MWExceptionHandler\prettyPrintTrace
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
Definition: MWExceptionHandler.php:416
MediaWiki\Logger\Monolog\LineFormatter\format
format(array $record)
Definition: LineFormatter.php:67
$output
$output
Definition: SyntaxHighlight.php:335
MediaWiki\Logger\Monolog\LineFormatter\normalizeExceptionArray
normalizeExceptionArray(array $e)
Convert an array of Throwable data to a string.
Definition: LineFormatter.php:137
MediaWiki\Logger\Monolog\LineFormatter
Formats incoming records into a one-line string.
Definition: LineFormatter.php:44
MediaWiki\Logger\Monolog\LineFormatter\__construct
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
Definition: LineFormatter.php:53