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 ): string {
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 
103  protected function normalizeException( Throwable $e, int $depth = 0 ): string {
104  // Can't use typehint. Must match Monolog\Formatter\LineFormatter::normalizeException($e)
105  return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
106  }
107 
114  protected function exceptionAsArray( Throwable $e ) {
115  $out = [
116  'class' => get_class( $e ),
117  'message' => $e->getMessage(),
118  'code' => $e->getCode(),
119  'file' => $e->getFile(),
120  'line' => $e->getLine(),
121  'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
122  ];
123 
124  $prev = $e->getPrevious();
125  if ( $prev ) {
126  $out['previous'] = $this->exceptionAsArray( $prev );
127  }
128 
129  return $out;
130  }
131 
138  protected function normalizeExceptionArray( array $e ) {
139  $defaults = [
140  'class' => 'Unknown',
141  'file' => 'unknown',
142  'line' => null,
143  'message' => 'unknown',
144  'trace' => [],
145  ];
146  $e = array_merge( $defaults, $e );
147 
148  $which = is_a( $e['class'], Error::class, true ) ? 'Error' : 'Exception';
149  $str = "\n[$which {$e['class']}] (" .
150  "{$e['file']}:{$e['line']}) {$e['message']}";
151 
152  if ( $this->includeStacktraces && $e['trace'] ) {
153  $str .= "\n" .
154  MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
155  }
156 
157  if ( isset( $e['previous'] ) ) {
158  $prev = $e['previous'];
159  while ( $prev ) {
160  $prev = array_merge( $defaults, $prev );
161  $which = is_a( $prev['class'], Error::class, true ) ? 'Error' : 'Exception';
162  $str .= "\nCaused by: [$which {$prev['class']}] (" .
163  "{$prev['file']}:{$prev['line']}) {$prev['message']}";
164 
165  if ( $this->includeStacktraces && $prev['trace'] ) {
166  $str .= "\n" .
168  $prev['trace'], ' '
169  );
170  }
171 
172  $prev = $prev['previous'] ?? null;
173  }
174  }
175  return $str;
176  }
177 }
MWExceptionHandler
Handler class for MWExceptions.
Definition: MWExceptionHandler.php:32
MWExceptionHandler\redactTrace
static redactTrace(array $trace)
Redact a stacktrace generated by Throwable::getTrace(), debug_backtrace() or similar means.
Definition: MWExceptionHandler.php:449
MediaWiki\Logger\Monolog
Definition: BufferHandler.php:23
MediaWiki\Logger\Monolog\LineFormatter\normalizeException
normalizeException(Throwable $e, int $depth=0)
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:388
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:138
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:114
MediaWiki\Logger\Monolog\LineFormatter\__construct
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
Definition: LineFormatter.php:52