MediaWiki  1.29.1
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 
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 exceptions 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 Exception ) {
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  return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
104  }
105 
112  protected function exceptionAsArray( Exception $e ) {
113  $out = [
114  'class' => get_class( $e ),
115  'message' => $e->getMessage(),
116  'code' => $e->getCode(),
117  'file' => $e->getFile(),
118  'line' => $e->getLine(),
119  'trace' => MWExceptionHandler::redactTrace( $e->getTrace() ),
120  ];
121 
122  $prev = $e->getPrevious();
123  if ( $prev ) {
124  $out['previous'] = $this->exceptionAsArray( $prev );
125  }
126 
127  return $out;
128  }
129 
136  protected function normalizeExceptionArray( array $e ) {
137  $defaults = [
138  'class' => 'Unknown',
139  'file' => 'unknown',
140  'line' => null,
141  'message' => 'unknown',
142  'trace' => [],
143  ];
144  $e = array_merge( $defaults, $e );
145 
146  $str = "\n[Exception {$e['class']}] (" .
147  "{$e['file']}:{$e['line']}) {$e['message']}";
148 
149  if ( $this->includeStacktraces && $e['trace'] ) {
150  $str .= "\n" .
151  MWExceptionHandler::prettyPrintTrace( $e['trace'], ' ' );
152  }
153 
154  if ( isset( $e['previous'] ) ) {
155  $prev = $e['previous'];
156  while ( $prev ) {
157  $prev = array_merge( $defaults, $prev );
158  $str .= "\nCaused by: [Exception {$prev['class']}] (" .
159  "{$prev['file']}:{$prev['line']}) {$prev['message']}";
160 
161  if ( $this->includeStacktraces && $prev['trace'] ) {
162  $str .= "\n" .
164  $prev['trace'], ' '
165  );
166  }
167 
168  $prev = isset( $prev['previous'] ) ? $prev['previous'] : null;
169  }
170  }
171  return $str;
172  }
173 }
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:386
MediaWiki\Logger\Monolog\LineFormatter\exceptionAsArray
exceptionAsArray(Exception $e)
Convert an exception to an array of structured data.
Definition: LineFormatter.php:112
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:102
MWExceptionHandler\prettyPrintTrace
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
Definition: MWExceptionHandler.php:325
$output
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition: hooks.txt:1049
MediaWiki\Logger\Monolog\LineFormatter\format
format(array $record)
{}
Definition: LineFormatter.php:66
MediaWiki\Logger\Monolog\LineFormatter\normalizeExceptionArray
normalizeExceptionArray(array $e)
Convert an array of Exception data to a string.
Definition: LineFormatter.php:136
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
MediaWiki\Logger\Monolog\LineFormatter
Formats incoming records into a one-line string.
Definition: LineFormatter.php:43
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1630
MediaWiki\Logger\Monolog\LineFormatter\__construct
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
Definition: LineFormatter.php:52
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