MediaWiki REL1_31
LineFormatter.php
Go to the documentation of this file.
1<?php
22
23use Exception;
24use Monolog\Formatter\LineFormatter as MonologLineFormatter;
26
42class 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" .
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Handler class for MWExceptions.
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
static redactTrace(array $trace)
Redact a stacktrace generated by Exception::getTrace(), debug_backtrace() or similar means.
Formats incoming records into a one-line string.
exceptionAsArray(Exception $e)
Convert an exception to an array of structured data.
__construct( $format=null, $dateFormat=null, $allowInlineLineBreaks=false, $ignoreEmptyContextAndExtra=false, $includeStacktraces=false)
normalizeException( $e)
Convert an Exception to a string.
normalizeExceptionArray(array $e)
Convert an array of Exception data to a string.
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
the array() calling protocol came about after MediaWiki 1.4rc1.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2255
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:864
returning false will NOT prevent logging $e
Definition hooks.txt:2176
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:37