MediaWiki  1.31.0
LineFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
23 use Exception;
24 use Monolog\Formatter\LineFormatter as MonologLineFormatter;
26 
42 class LineFormatter extends MonologLineFormatter {
43 
51  public function __construct(
52  $format = null, $dateFormat = null, $allowInlineLineBreaks = false,
53  $ignoreEmptyContextAndExtra = false, $includeStacktraces = false
54  ) {
55  parent::__construct(
56  $format, $dateFormat, $allowInlineLineBreaks,
57  $ignoreEmptyContextAndExtra
58  );
59  $this->includeStacktraces( $includeStacktraces );
60  }
61 
65  public function format( array $record ) {
66  // Drop the 'private' flag from the context
67  unset( $record['context']['private'] );
68 
69  // Handle exceptions specially: pretty format and remove from context
70  // Will be output for a '%exception%' placeholder in format
71  $prettyException = '';
72  if ( isset( $record['context']['exception'] ) &&
73  strpos( $this->format, '%exception%' ) !== false
74  ) {
75  $e = $record['context']['exception'];
76  unset( $record['context']['exception'] );
77 
78  if ( $e instanceof Exception ) {
79  $prettyException = $this->normalizeException( $e );
80  } elseif ( is_array( $e ) ) {
81  $prettyException = $this->normalizeExceptionArray( $e );
82  } else {
83  $prettyException = $this->stringify( $e );
84  }
85  }
86 
87  $output = parent::format( $record );
88 
89  if ( strpos( $output, '%exception%' ) !== false ) {
90  $output = str_replace( '%exception%', $prettyException, $output );
91  }
92  return $output;
93  }
94 
101  protected function normalizeException( $e ) {
102  return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
103  }
104 
111  protected function exceptionAsArray( Exception $e ) {
112  $out = [
113  'class' => get_class( $e ),
114  'message' => $e->getMessage(),
115  'code' => $e->getCode(),
116  'file' => $e->getFile(),
117  'line' => $e->getLine(),
118  'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
119  ];
120 
121  $prev = $e->getPrevious();
122  if ( $prev ) {
123  $out['previous'] = $this->exceptionAsArray( $prev );
124  }
125 
126  return $out;
127  }
128 
135  protected function normalizeExceptionArray( array $e ) {
136  $defaults = [
137  'class' => 'Unknown',
138  'file' => 'unknown',
139  'line' => null,
140  'message' => 'unknown',
141  'trace' => [],
142  ];
143  $e = array_merge( $defaults, $e );
144 
145  $str = "\n[Exception {$e['class']}] (" .
146  "{$e['file']}:{$e['line']}) {$e['message']}";
147 
148  if ( $this->includeStacktraces && $e['trace'] ) {
149  $str .= "\n" .
150  MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
151  }
152 
153  if ( isset( $e['previous'] ) ) {
154  $prev = $e['previous'];
155  while ( $prev ) {
156  $prev = array_merge( $defaults, $prev );
157  $str .= "\nCaused by: [Exception {$prev['class']}] (" .
158  "{$prev['file']}:{$prev['line']}) {$prev['message']}";
159 
160  if ( $this->includeStacktraces && $prev['trace'] ) {
161  $str .= "\n" .
163  $prev['trace'], ' '
164  );
165  }
166 
167  $prev = isset( $prev['previous'] ) ? $prev['previous'] : null;
168  }
169  }
170  return $str;
171  }
172 }
MWExceptionHandler
Handler class for MWExceptions.
Definition: MWExceptionHandler.php:30
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MWExceptionHandler\redactTrace
static redactTrace(array $trace)
Redact a stacktrace generated by Exception::getTrace(), debug_backtrace() or similar means.
Definition: MWExceptionHandler.php:416
MediaWiki\Logger\Monolog\LineFormatter\exceptionAsArray
exceptionAsArray(Exception $e)
Convert an exception to an array of structured data.
Definition: LineFormatter.php:111
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Logger\Monolog\LineFormatter\normalizeException
normalizeException( $e)
Convert an Exception to a string.
Definition: LineFormatter.php:101
MWExceptionHandler\prettyPrintTrace
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
Definition: MWExceptionHandler.php:355
MediaWiki\Logger\Monolog\LineFormatter\format
format(array $record)
@inheritDoc
Definition: LineFormatter.php:65
$output
$output
Definition: SyntaxHighlight.php:338
MediaWiki\Logger\Monolog\LineFormatter\normalizeExceptionArray
normalizeExceptionArray(array $e)
Convert an array of Exception data to a string.
Definition: LineFormatter.php:135
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2163
MediaWiki\Logger\Monolog\LineFormatter
Formats incoming records into a one-line string.
Definition: LineFormatter.php:42
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1649
MediaWiki\Logger\Monolog\LineFormatter\__construct
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
Definition: LineFormatter.php:51
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:783