MediaWiki  1.27.2
LegacyHandler.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
28 
50 class LegacyHandler extends AbstractProcessingHandler {
51 
56  protected $uri;
57 
62  protected $useLegacyFilter;
63 
68  protected $sink;
69 
73  protected $error;
74 
78  protected $host;
79 
83  protected $port;
84 
88  protected $prefix;
89 
96  public function __construct(
97  $stream,
98  $useLegacyFilter = false,
99  $level = Logger::DEBUG,
100  $bubble = true
101  ) {
102  parent::__construct( $level, $bubble );
103  $this->uri = $stream;
104  $this->useLegacyFilter = $useLegacyFilter;
105  }
106 
110  protected function openSink() {
111  if ( !$this->uri ) {
112  throw new LogicException(
113  'Missing stream uri, the stream can not be opened.' );
114  }
115  $this->error = null;
116  set_error_handler( [ $this, 'errorTrap' ] );
117 
118  if ( substr( $this->uri, 0, 4 ) == 'udp:' ) {
119  $parsed = parse_url( $this->uri );
120  if ( !isset( $parsed['host'] ) ) {
121  throw new UnexpectedValueException( sprintf(
122  'Udp transport "%s" must specify a host', $this->uri
123  ) );
124  }
125  if ( !isset( $parsed['port'] ) ) {
126  throw new UnexpectedValueException( sprintf(
127  'Udp transport "%s" must specify a port', $this->uri
128  ) );
129  }
130 
131  $this->host = $parsed['host'];
132  $this->port = $parsed['port'];
133  $this->prefix = '';
134 
135  if ( isset( $parsed['path'] ) ) {
136  $this->prefix = ltrim( $parsed['path'], '/' );
137  }
138 
139  if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
140  $domain = AF_INET6;
141 
142  } else {
143  $domain = AF_INET;
144  }
145 
146  $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
147 
148  } else {
149  $this->sink = fopen( $this->uri, 'a' );
150  }
151  restore_error_handler();
152 
153  if ( !is_resource( $this->sink ) ) {
154  $this->sink = null;
155  throw new UnexpectedValueException( sprintf(
156  'The stream or file "%s" could not be opened: %s',
157  $this->uri, $this->error
158  ) );
159  }
160  }
161 
167  protected function errorTrap( $code, $msg ) {
168  $this->error = $msg;
169  }
170 
175  protected function useUdp() {
176  return $this->host !== null;
177  }
178 
179  protected function write( array $record ) {
180  if ( $this->useLegacyFilter &&
182  $record['channel'], $record['message'],
183  $record['level'], $record
184  ) ) {
185  // Do not write record if we are enforcing legacy rules and they
186  // do not pass this message. This used to be done in isHandling(),
187  // but Monolog 1.12.0 made a breaking change that removed access
188  // to the needed channel and context information.
189  return;
190  }
191 
192  if ( $this->sink === null ) {
193  $this->openSink();
194  }
195 
196  $text = (string)$record['formatted'];
197  if ( $this->useUdp() ) {
198 
199  // Clean it up for the multiplexer
200  if ( $this->prefix !== '' ) {
201  $leader = ( $this->prefix === '{channel}' ) ?
202  $record['channel'] : $this->prefix;
203  $text = preg_replace( '/^/m', "{$leader} ", $text );
204 
205  // Limit to 64KB
206  if ( strlen( $text ) > 65506 ) {
207  $text = substr( $text, 0, 65506 );
208  }
209 
210  if ( substr( $text, -1 ) != "\n" ) {
211  $text .= "\n";
212  }
213 
214  } elseif ( strlen( $text ) > 65507 ) {
215  $text = substr( $text, 0, 65507 );
216  }
217 
218  socket_sendto(
219  $this->sink, $text, strlen( $text ), 0, $this->host, $this->port
220  );
221 
222  } else {
223  fwrite( $this->sink, $text );
224  }
225  }
226 
227  public function close() {
228  if ( is_resource( $this->sink ) ) {
229  if ( $this->useUdp() ) {
230  socket_close( $this->sink );
231 
232  } else {
233  fclose( $this->sink );
234  }
235  }
236  $this->sink = null;
237  }
238 }
MediaWiki Logger LegacyLogger
Definition: logger.txt:54
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
errorTrap($code, $msg)
Custom error handler.
static shouldEmit($channel, $message, $level, $context)
Determine if the given message should be emitted or not.
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:177
useUdp()
Should we use UDP to send messages to the sink?
$useLegacyFilter
Filter log events using legacy rules.
openSink()
Open the log sink described by our stream URI.
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:762
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
Log handler that replicates the behavior of MediaWiki's wfErrorLog() logging service.
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
__construct($stream, $useLegacyFilter=false, $level=Logger::DEBUG, $bubble=true)