MediaWiki  master
LineFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
23 use Error;
24 use Monolog\Formatter\LineFormatter as MonologLineFormatter;
26 use Throwable;
27 
43 class LineFormatter extends MonologLineFormatter {
44 
52  public function __construct(
53  $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
54  $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
55  ) {
56  parent::__construct(
57  $format, $dateFormat, $allowInlineLineBreaks,
58  $ignoreEmptyContextAndExtra
59  );
60  $this->includeStacktraces( $includeStacktraces );
61  }
62 
66  public function format( array $record ) {
67  // Drop the 'private' flag from the context
68  unset( $record['context']['private'] );
69 
70  // Handle throwables specially: pretty format and remove from context
71  // Will be output for a '%exception%' placeholder in format
72  $prettyException = '';
73  if ( isset( $record['context']['exception'] ) &&
74  strpos( $this->format, '%exception%' ) !== false
75  ) {
76  $e = $record['context']['exception'];
77  unset( $record['context']['exception'] );
78 
79  if ( $e instanceof Throwable ) {
80  $prettyException = $this->normalizeException( $e );
81  } elseif ( is_array( $e ) ) {
82  $prettyException = $this->normalizeExceptionArray( $e );
83  } else {
84  $prettyException = $this->stringify( $e );
85  }
86  }
87 
88  $output = parent::format( $record );
89 
90  if ( strpos( $output, '%exception%' ) !== false ) {
91  $output = str_replace( '%exception%', $prettyException, $output );
92  }
93  return $output;
94  }
95 
102  protected function normalizeException( $e ) {
103  // Can't use typehint. Must match Monolog\Formatter\LineFormatter::normalizeException($e)
104  return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
105  }
106 
113  protected function exceptionAsArray( Throwable $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:31
MWExceptionHandler\redactTrace
static redactTrace(array $trace)
Redact a stacktrace generated by Throwable::getTrace(), debug_backtrace() or similar means.
Definition: MWExceptionHandler.php:437
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
MediaWiki\Logger\Monolog\LineFormatter\normalizeException
normalizeException( $e)
Convert a Throwable to a string.
Definition: LineFormatter.php:102
MWExceptionHandler\prettyPrintTrace
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
Definition: MWExceptionHandler.php:376
MediaWiki\Logger\Monolog\LineFormatter\format
format(array $record)
Definition: LineFormatter.php:66
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:43
MediaWiki\Logger\Monolog\LineFormatter\exceptionAsArray
exceptionAsArray(Throwable $e)
Convert a throwable to an array of structured data.
Definition: LineFormatter.php:113
MediaWiki\Logger\Monolog\LineFormatter\__construct
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
Definition: LineFormatter.php:52