ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Monolog\Handler\SocketHandler Class Reference

Stores to any socket - uses fsockopen() or pfsockopen(). More...

+ Inheritance diagram for Monolog\Handler\SocketHandler:
+ Collaboration diagram for Monolog\Handler\SocketHandler:

Public Member Functions

 __construct ($connectionString, $level=Logger::DEBUG, $bubble=true)
 
 close ()
 We will not close a PersistentSocket instance so it can be reused in other requests. More...
 
 closeSocket ()
 Close socket, if open. More...
 
 setPersistent ($persistent)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write timeout. More...
 
 setWritingTimeout ($seconds)
 Set writing timeout. More...
 
 getConnectionString ()
 Get current connection string. More...
 
 isPersistent ()
 Get persistent setting. More...
 
 getConnectionTimeout ()
 Get current connection timeout setting. More...
 
 getTimeout ()
 Get current in-transfer timeout. More...
 
 getWritingTimeout ()
 Get current local writing timeout. More...
 
 isConnected ()
 Check to see if the socket is currently available. More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 handle (array $record)
 {Handles a record.All records may be passed to this method, and the handler should discard those that it does not want to handle.The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.
Parameters
array$recordThe record to handle
Returns
Boolean true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
} More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
Boolean
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 

Protected Member Functions

 write (array $record)
 Connect (if necessary) and write to the socket. More...
 
 pfsockopen ()
 Wrapper to allow mocking. More...
 
 fsockopen ()
 Wrapper to allow mocking. More...
 
 streamSetTimeout ()
 Wrapper to allow mocking. More...
 
 fwrite ($data)
 Wrapper to allow mocking. More...
 
 streamGetMetadata ()
 Wrapper to allow mocking. More...
 
 generateDataStream ($record)
 
 getResource ()
 
- Protected Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 write (array $record)
 Writes the record down to the log of the implementing handler. More...
 
 processRecord (array $record)
 Processes a record. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Private Member Functions

 validateTimeout ($value)
 
 connectIfNotConnected ()
 
 connect ()
 
 createSocketResource ()
 
 setSocketTimeout ()
 
 writeToSocket ($data)
 
 writingIsTimedOut ($sent)
 

Private Attributes

 $connectionString
 
 $connectionTimeout
 
 $resource
 
 $timeout = 0
 
 $writingTimeout = 10
 
 $lastSentBytes = null
 
 $persistent = false
 
 $errno
 
 $errstr
 
 $lastWritingAt
 

Additional Inherited Members

- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Detailed Description

Stores to any socket - uses fsockopen() or pfsockopen().

Author
Pablo de Leon Belloc pablo.nosp@m.lb@g.nosp@m.mail..nosp@m.com
See also
http://php.net/manual/en/function.fsockopen.php

Definition at line 22 of file SocketHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\SocketHandler::__construct (   $connectionString,
  $level = Logger::DEBUG,
  $bubble = true 
)
Parameters
string$connectionStringSocket connection string
int$levelThe minimum logging level at which this handler will be triggered
Boolean$bubbleWhether the messages that are handled can bubble up the stack or not

Definition at line 40 of file SocketHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\SocketHandler\$connectionString, and Monolog\Handler\AbstractHandler\$level.

41  {
42  parent::__construct($level, $bubble);
43  $this->connectionString = $connectionString;
44  $this->connectionTimeout = (float) ini_get('default_socket_timeout');
45  }

Member Function Documentation

◆ close()

Monolog\Handler\SocketHandler::close ( )

We will not close a PersistentSocket instance so it can be reused in other requests.

Definition at line 65 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\closeSocket(), and Monolog\Handler\SocketHandler\isPersistent().

66  {
67  if (!$this->isPersistent()) {
68  $this->closeSocket();
69  }
70  }
isPersistent()
Get persistent setting.
closeSocket()
Close socket, if open.
+ Here is the call graph for this function:

◆ closeSocket()

Monolog\Handler\SocketHandler::closeSocket ( )

Close socket, if open.

Definition at line 75 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\close(), Monolog\Handler\FleepHookHandler\write(), Monolog\Handler\FlowdockHandler\write(), Monolog\Handler\PushoverHandler\write(), Monolog\Handler\HipChatHandler\write(), Monolog\Handler\SlackHandler\write(), and Monolog\Handler\SocketHandler\writingIsTimedOut().

76  {
77  if (is_resource($this->resource)) {
78  fclose($this->resource);
79  $this->resource = null;
80  }
81  }
+ Here is the caller graph for this function:

◆ connect()

Monolog\Handler\SocketHandler::connect ( )
private

Definition at line 267 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\createSocketResource(), and Monolog\Handler\SocketHandler\setSocketTimeout().

Referenced by Monolog\Handler\SocketHandler\connectIfNotConnected().

268  {
269  $this->createSocketResource();
270  $this->setSocketTimeout();
271  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ connectIfNotConnected()

Monolog\Handler\SocketHandler::connectIfNotConnected ( )
private

Definition at line 246 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\connect(), and Monolog\Handler\SocketHandler\isConnected().

Referenced by Monolog\Handler\SocketHandler\write().

247  {
248  if ($this->isConnected()) {
249  return;
250  }
251  $this->connect();
252  }
isConnected()
Check to see if the socket is currently available.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createSocketResource()

Monolog\Handler\SocketHandler::createSocketResource ( )
private

Definition at line 273 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$resource, Monolog\Handler\SocketHandler\fsockopen(), Monolog\Handler\SocketHandler\isPersistent(), and Monolog\Handler\SocketHandler\pfsockopen().

Referenced by Monolog\Handler\SocketHandler\connect().

274  {
275  if ($this->isPersistent()) {
276  $resource = $this->pfsockopen();
277  } else {
278  $resource = $this->fsockopen();
279  }
280  if (!$resource) {
281  throw new \UnexpectedValueException("Failed connecting to $this->connectionString ($this->errno: $this->errstr)");
282  }
283  $this->resource = $resource;
284  }
fsockopen()
Wrapper to allow mocking.
isPersistent()
Get persistent setting.
pfsockopen()
Wrapper to allow mocking.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fsockopen()

Monolog\Handler\SocketHandler::fsockopen ( )
protected

Wrapper to allow mocking.

Definition at line 204 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\createSocketResource().

205  {
206  return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
207  }
fsockopen()
Wrapper to allow mocking.
+ Here is the caller graph for this function:

◆ fwrite()

Monolog\Handler\SocketHandler::fwrite (   $data)
protected

Wrapper to allow mocking.

Definition at line 225 of file SocketHandler.php.

References $data.

Referenced by Monolog\Handler\SocketHandler\writeToSocket().

226  {
227  return @fwrite($this->resource, $data);
228  }
fwrite($data)
Wrapper to allow mocking.
+ Here is the caller graph for this function:

◆ generateDataStream()

Monolog\Handler\SocketHandler::generateDataStream (   $record)
protected

Definition at line 254 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\write().

255  {
256  return (string) $record['formatted'];
257  }
+ Here is the caller graph for this function:

◆ getConnectionString()

Monolog\Handler\SocketHandler::getConnectionString ( )

Get current connection string.

Returns
string

Definition at line 135 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$connectionString.

136  {
138  }

◆ getConnectionTimeout()

Monolog\Handler\SocketHandler::getConnectionTimeout ( )

Get current connection timeout setting.

Returns
float

Definition at line 155 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$connectionTimeout.

156  {
158  }

◆ getResource()

Monolog\Handler\SocketHandler::getResource ( )
protected
Returns
resource|null

Definition at line 262 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$resource.

Referenced by Monolog\Handler\SlackHandler\write().

263  {
264  return $this->resource;
265  }
+ Here is the caller graph for this function:

◆ getTimeout()

Monolog\Handler\SocketHandler::getTimeout ( )

Get current in-transfer timeout.

Returns
float

Definition at line 165 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$timeout.

166  {
167  return $this->timeout;
168  }

◆ getWritingTimeout()

Monolog\Handler\SocketHandler::getWritingTimeout ( )

Get current local writing timeout.

Returns
float

Definition at line 175 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$writingTimeout.

176  {
177  return $this->writingTimeout;
178  }

◆ isConnected()

Monolog\Handler\SocketHandler::isConnected ( )

Check to see if the socket is currently available.

UDP might appear to be connected but might fail when writing. See http://php.net/fsockopen for details.

Returns
bool

Definition at line 187 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\connectIfNotConnected(), and Monolog\Handler\SocketHandler\writeToSocket().

188  {
189  return is_resource($this->resource)
190  && !feof($this->resource); // on TCP - other party can close connection.
191  }
+ Here is the caller graph for this function:

◆ isPersistent()

Monolog\Handler\SocketHandler::isPersistent ( )

Get persistent setting.

Returns
bool

Definition at line 145 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$persistent.

Referenced by Monolog\Handler\SocketHandler\close(), and Monolog\Handler\SocketHandler\createSocketResource().

146  {
147  return $this->persistent;
148  }
+ Here is the caller graph for this function:

◆ pfsockopen()

Monolog\Handler\SocketHandler::pfsockopen ( )
protected

Wrapper to allow mocking.

Definition at line 196 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\createSocketResource().

197  {
198  return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
199  }
pfsockopen()
Wrapper to allow mocking.
+ Here is the caller graph for this function:

◆ setConnectionTimeout()

Monolog\Handler\SocketHandler::setConnectionTimeout (   $seconds)

Set connection timeout.

Only has effect before we connect.

Parameters
float$seconds
See also
http://php.net/manual/en/function.fsockopen.php

Definition at line 100 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\validateTimeout().

101  {
102  $this->validateTimeout($seconds);
103  $this->connectionTimeout = (float) $seconds;
104  }
+ Here is the call graph for this function:

◆ setPersistent()

Monolog\Handler\SocketHandler::setPersistent (   $persistent)

Set socket connection to nbe persistent.

It only has effect before the connection is initiated.

Parameters
bool$persistent

Definition at line 88 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$persistent.

89  {
90  $this->persistent = (boolean) $persistent;
91  }

◆ setSocketTimeout()

Monolog\Handler\SocketHandler::setSocketTimeout ( )
private

Definition at line 286 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\streamSetTimeout().

Referenced by Monolog\Handler\SocketHandler\connect().

287  {
288  if (!$this->streamSetTimeout()) {
289  throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
290  }
291  }
streamSetTimeout()
Wrapper to allow mocking.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTimeout()

Monolog\Handler\SocketHandler::setTimeout (   $seconds)

Set write timeout.

Only has effect before we connect.

Parameters
float$seconds
See also
http://php.net/manual/en/function.stream-set-timeout.php

Definition at line 113 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\validateTimeout().

114  {
115  $this->validateTimeout($seconds);
116  $this->timeout = (float) $seconds;
117  }
+ Here is the call graph for this function:

◆ setWritingTimeout()

Monolog\Handler\SocketHandler::setWritingTimeout (   $seconds)

Set writing timeout.

Only has effect during connection in the writing cycle.

Parameters
float$seconds0 for no timeout

Definition at line 124 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\validateTimeout().

125  {
126  $this->validateTimeout($seconds);
127  $this->writingTimeout = (float) $seconds;
128  }
+ Here is the call graph for this function:

◆ streamGetMetadata()

Monolog\Handler\SocketHandler::streamGetMetadata ( )
protected

Wrapper to allow mocking.

Definition at line 233 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\writeToSocket().

234  {
235  return stream_get_meta_data($this->resource);
236  }
+ Here is the caller graph for this function:

◆ streamSetTimeout()

Monolog\Handler\SocketHandler::streamSetTimeout ( )
protected

Wrapper to allow mocking.

See also
http://php.net/manual/en/function.stream-set-timeout.php

Definition at line 214 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\setSocketTimeout().

215  {
216  $seconds = floor($this->timeout);
217  $microseconds = round(($this->timeout - $seconds) * 1e6);
218 
219  return stream_set_timeout($this->resource, $seconds, $microseconds);
220  }
+ Here is the caller graph for this function:

◆ validateTimeout()

Monolog\Handler\SocketHandler::validateTimeout (   $value)
private

Definition at line 238 of file SocketHandler.php.

References $ok.

Referenced by Monolog\Handler\SocketHandler\setConnectionTimeout(), Monolog\Handler\SocketHandler\setTimeout(), and Monolog\Handler\SocketHandler\setWritingTimeout().

239  {
240  $ok = filter_var($value, FILTER_VALIDATE_FLOAT);
241  if ($ok === false || $value < 0) {
242  throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)");
243  }
244  }
+ Here is the caller graph for this function:

◆ write()

Monolog\Handler\SocketHandler::write ( array  $record)
protected

Connect (if necessary) and write to the socket.

Parameters
array$record
Exceptions

Definition at line 55 of file SocketHandler.php.

References $data, Monolog\Handler\SocketHandler\connectIfNotConnected(), Monolog\Handler\SocketHandler\generateDataStream(), and Monolog\Handler\SocketHandler\writeToSocket().

+ Here is the call graph for this function:

◆ writeToSocket()

Monolog\Handler\SocketHandler::writeToSocket (   $data)
private

Definition at line 293 of file SocketHandler.php.

References $data, Monolog\Handler\SocketHandler\fwrite(), Monolog\Handler\SocketHandler\isConnected(), Monolog\Handler\SocketHandler\streamGetMetadata(), and Monolog\Handler\SocketHandler\writingIsTimedOut().

Referenced by Monolog\Handler\SocketHandler\write().

294  {
295  $length = strlen($data);
296  $sent = 0;
297  $this->lastSentBytes = $sent;
298  while ($this->isConnected() && $sent < $length) {
299  if (0 == $sent) {
300  $chunk = $this->fwrite($data);
301  } else {
302  $chunk = $this->fwrite(substr($data, $sent));
303  }
304  if ($chunk === false) {
305  throw new \RuntimeException("Could not write to socket");
306  }
307  $sent += $chunk;
308  $socketInfo = $this->streamGetMetadata();
309  if ($socketInfo['timed_out']) {
310  throw new \RuntimeException("Write timed-out");
311  }
312 
313  if ($this->writingIsTimedOut($sent)) {
314  throw new \RuntimeException("Write timed-out, no data sent for `{$this->writingTimeout}` seconds, probably we got disconnected (sent $sent of $length)");
315  }
316  }
317  if (!$this->isConnected() && $sent < $length) {
318  throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
319  }
320  }
fwrite($data)
Wrapper to allow mocking.
isConnected()
Check to see if the socket is currently available.
streamGetMetadata()
Wrapper to allow mocking.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writingIsTimedOut()

Monolog\Handler\SocketHandler::writingIsTimedOut (   $sent)
private

Definition at line 322 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$writingTimeout, Monolog\Handler\SocketHandler\closeSocket(), and time.

Referenced by Monolog\Handler\SocketHandler\writeToSocket().

323  {
324  $writingTimeout = (int) floor($this->writingTimeout);
325  if (0 === $writingTimeout) {
326  return false;
327  }
328 
329  if ($sent !== $this->lastSentBytes) {
330  $this->lastWritingAt = time();
331  $this->lastSentBytes = $sent;
332 
333  return false;
334  } else {
335  usleep(100);
336  }
337 
338  if ((time() - $this->lastWritingAt) >= $writingTimeout) {
339  $this->closeSocket();
340 
341  return true;
342  }
343 
344  return false;
345  }
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
closeSocket()
Close socket, if open.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $connectionString

◆ $connectionTimeout

Monolog\Handler\SocketHandler::$connectionTimeout
private

◆ $errno

Monolog\Handler\SocketHandler::$errno
private

Definition at line 31 of file SocketHandler.php.

◆ $errstr

Monolog\Handler\SocketHandler::$errstr
private

Definition at line 32 of file SocketHandler.php.

◆ $lastSentBytes

Monolog\Handler\SocketHandler::$lastSentBytes = null
private

Definition at line 29 of file SocketHandler.php.

◆ $lastWritingAt

Monolog\Handler\SocketHandler::$lastWritingAt
private

Definition at line 33 of file SocketHandler.php.

◆ $persistent

Monolog\Handler\SocketHandler::$persistent = false
private

◆ $resource

Monolog\Handler\SocketHandler::$resource
private

◆ $timeout

Monolog\Handler\SocketHandler::$timeout = 0
private

Definition at line 27 of file SocketHandler.php.

Referenced by Monolog\Handler\SocketHandler\getTimeout().

◆ $writingTimeout

Monolog\Handler\SocketHandler::$writingTimeout = 10
private

The documentation for this class was generated from the following file: