MediaWiki  1.30.0
LegacyHandler.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
23 use LogicException;
25 use Monolog\Handler\AbstractProcessingHandler;
26 use Monolog\Logger;
27 use UnexpectedValueException;
28 
49 class LegacyHandler extends AbstractProcessingHandler {
50 
55  protected $uri;
56 
61  protected $useLegacyFilter;
62 
67  protected $sink;
68 
72  protected $error;
73 
77  protected $host;
78 
82  protected $port;
83 
87  protected $prefix;
88 
95  public function __construct(
96  $stream,
97  $useLegacyFilter = false,
98  $level = Logger::DEBUG,
99  $bubble = true
100  ) {
101  parent::__construct( $level, $bubble );
102  $this->uri = $stream;
103  $this->useLegacyFilter = $useLegacyFilter;
104  }
105 
109  protected function openSink() {
110  if ( !$this->uri ) {
111  throw new LogicException(
112  'Missing stream uri, the stream can not be opened.' );
113  }
114  $this->error = null;
115  set_error_handler( [ $this, 'errorTrap' ] );
116 
117  if ( substr( $this->uri, 0, 4 ) == 'udp:' ) {
118  $parsed = parse_url( $this->uri );
119  if ( !isset( $parsed['host'] ) ) {
120  throw new UnexpectedValueException( sprintf(
121  'Udp transport "%s" must specify a host', $this->uri
122  ) );
123  }
124  if ( !isset( $parsed['port'] ) ) {
125  throw new UnexpectedValueException( sprintf(
126  'Udp transport "%s" must specify a port', $this->uri
127  ) );
128  }
129 
130  $this->host = $parsed['host'];
131  $this->port = $parsed['port'];
132  $this->prefix = '';
133 
134  if ( isset( $parsed['path'] ) ) {
135  $this->prefix = ltrim( $parsed['path'], '/' );
136  }
137 
138  if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
139  $domain = AF_INET6;
140 
141  } else {
142  $domain = AF_INET;
143  }
144 
145  $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
146 
147  } else {
148  $this->sink = fopen( $this->uri, 'a' );
149  }
150  restore_error_handler();
151 
152  if ( !is_resource( $this->sink ) ) {
153  $this->sink = null;
154  throw new UnexpectedValueException( sprintf(
155  'The stream or file "%s" could not be opened: %s',
156  $this->uri, $this->error
157  ) );
158  }
159  }
160 
166  protected function errorTrap( $code, $msg ) {
167  $this->error = $msg;
168  }
169 
174  protected function useUdp() {
175  return $this->host !== null;
176  }
177 
178  protected function write( array $record ) {
179  if ( $this->useLegacyFilter &&
181  $record['channel'], $record['message'],
182  $record['level'], $record
183  ) ) {
184  // Do not write record if we are enforcing legacy rules and they
185  // do not pass this message. This used to be done in isHandling(),
186  // but Monolog 1.12.0 made a breaking change that removed access
187  // to the needed channel and context information.
188  return;
189  }
190 
191  if ( $this->sink === null ) {
192  $this->openSink();
193  }
194 
195  $text = (string)$record['formatted'];
196  if ( $this->useUdp() ) {
197  // Clean it up for the multiplexer
198  if ( $this->prefix !== '' ) {
199  $leader = ( $this->prefix === '{channel}' ) ?
200  $record['channel'] : $this->prefix;
201  $text = preg_replace( '/^/m', "{$leader} ", $text );
202 
203  // Limit to 64KB
204  if ( strlen( $text ) > 65506 ) {
205  $text = substr( $text, 0, 65506 );
206  }
207 
208  if ( substr( $text, -1 ) != "\n" ) {
209  $text .= "\n";
210  }
211 
212  } elseif ( strlen( $text ) > 65507 ) {
213  $text = substr( $text, 0, 65507 );
214  }
215 
216  socket_sendto(
217  $this->sink, $text, strlen( $text ), 0, $this->host, $this->port
218  );
219 
220  } else {
221  fwrite( $this->sink, $text );
222  }
223  }
224 
225  public function close() {
226  if ( is_resource( $this->sink ) ) {
227  if ( $this->useUdp() ) {
228  socket_close( $this->sink );
229 
230  } else {
231  fclose( $this->sink );
232  }
233  }
234  $this->sink = null;
235  }
236 }
MediaWiki\Logger\Monolog\LegacyHandler\__construct
__construct( $stream, $useLegacyFilter=false, $level=Logger::DEBUG, $bubble=true)
Definition: LegacyHandler.php:95
MediaWiki\Logger\Monolog\LegacyHandler\useUdp
useUdp()
Should we use UDP to send messages to the sink?
Definition: LegacyHandler.php:174
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
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
MediaWiki\Logger\Monolog\LegacyHandler\openSink
openSink()
Open the log sink described by our stream URI.
Definition: LegacyHandler.php:109
MediaWiki\Logger\Monolog\LegacyHandler
Log handler that replicates the behavior of MediaWiki's wfErrorLog() logging service.
Definition: LegacyHandler.php:49
MediaWiki\Logger\Monolog\LegacyHandler\$sink
$sink
Log sink.
Definition: LegacyHandler.php:67
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\LegacyHandler\errorTrap
errorTrap( $code, $msg)
Custom error handler.
Definition: LegacyHandler.php:166
MediaWiki\Logger\Monolog\LegacyHandler\$uri
$uri
Log sink descriptor.
Definition: LegacyHandler.php:55
MediaWiki\Logger\Monolog\LegacyHandler\close
close()
Definition: LegacyHandler.php:225
port
storage can be distributed across multiple and multiple web servers can use the same cache cluster *********************W A R N I N G ***********************Memcached has no security or authentication Please ensure that your server is appropriately and that the port(s) used for memcached servers are not publicly accessible. Otherwise
string
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition: hooks.txt:175
MediaWiki\Logger\Monolog\LegacyHandler\write
write(array $record)
Definition: LegacyHandler.php:178
MediaWiki\Logger\Monolog\LegacyHandler\$prefix
$prefix
Definition: LegacyHandler.php:87
$code
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 modifiable & $code
Definition: hooks.txt:781
MediaWiki\Logger\Monolog\LegacyHandler\$error
$error
Definition: LegacyHandler.php:72
MediaWiki\Logger\Monolog\LegacyHandler\$port
$port
Definition: LegacyHandler.php:82
error
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition: hooks.txt:2581
MediaWiki\Logger\LegacyLogger\shouldEmit
static shouldEmit( $channel, $message, $level, $context)
Determine if the given message should be emitted or not.
Definition: LegacyLogger.php:136
MediaWiki\Logger\Monolog\LegacyHandler\$host
$host
Definition: LegacyHandler.php:77
LegacyLogger
MediaWiki Logger LegacyLogger
Definition: logger.txt:54
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaWiki\Logger\Monolog\LegacyHandler\$useLegacyFilter
$useLegacyFilter
Filter log events using legacy rules.
Definition: LegacyHandler.php:61