ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 ()
 
 isHandling (array $record)
 Checks whether the given record will be handled by this handler. More...
 
 handle (array $record)
 Handles a record. More...
 
 handleBatch (array $records)
 Handles a set of records at once. More...
 
 pushProcessor ($callback)
 Adds a processor in the stack. More...
 
 popProcessor ()
 Removes the processor on top of the stack and returns it. More...
 
 setFormatter (FormatterInterface $formatter)
 Sets the formatter. More...
 
 getFormatter ()
 Gets the formatter. More...
 

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

Reimplemented in Monolog\Handler\FlowdockHandler, and Monolog\Handler\FleepHookHandler.

Definition at line 40 of file SocketHandler.php.

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

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

Member Function Documentation

◆ close()

Monolog\Handler\SocketHandler::close ( )

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

Reimplemented from Monolog\Handler\AbstractHandler.

Definition at line 65 of file SocketHandler.php.

66 {
67 if (!$this->isPersistent()) {
68 $this->closeSocket();
69 }
70 }
isPersistent()
Get persistent setting.
closeSocket()
Close socket, if open.

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

+ 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.

76 {
77 if (is_resource($this->resource)) {
78 fclose($this->resource);
79 $this->resource = null;
80 }
81 }

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

+ 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().

+ 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.

247 {
248 if ($this->isConnected()) {
249 return;
250 }
251 $this->connect();
252 }
isConnected()
Check to see if the socket is currently available.

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

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

+ 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.

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 }
pfsockopen()
Wrapper to allow mocking.
fsockopen()
Wrapper to allow mocking.

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

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

+ 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.

205 {
206 return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
207 }

References Monolog\Handler\SocketHandler\fsockopen().

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

+ Here is the call graph for this function:
+ 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.

226 {
227 return @fwrite($this->resource, $data);
228 }
fwrite($data)
Wrapper to allow mocking.

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateDataStream()

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

Reimplemented in Monolog\Handler\FleepHookHandler, Monolog\Handler\FlowdockHandler, Monolog\Handler\HipChatHandler, Monolog\Handler\LogEntriesHandler, Monolog\Handler\PushoverHandler, and Monolog\Handler\SlackHandler.

Definition at line 254 of file SocketHandler.php.

255 {
256 return (string) $record['formatted'];
257 }

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

+ 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.

136 {
138 }

References Monolog\Handler\SocketHandler\$connectionString.

◆ 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.

◆ getResource()

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

Definition at line 262 of file SocketHandler.php.

263 {
264 return $this->resource;
265 }

References Monolog\Handler\SocketHandler\$resource.

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

+ 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.

◆ 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.

◆ 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.

188 {
189 return is_resource($this->resource)
190 && !feof($this->resource); // on TCP - other party can close connection.
191 }

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

+ 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().

+ 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.

197 {
198 return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
199 }

References Monolog\Handler\SocketHandler\pfsockopen().

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

+ Here is the call graph for this function:
+ 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.

101 {
102 $this->validateTimeout($seconds);
103 $this->connectionTimeout = (float) $seconds;
104 }

References Monolog\Handler\SocketHandler\validateTimeout().

+ 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.

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

References Monolog\Handler\SocketHandler\$persistent.

◆ setSocketTimeout()

Monolog\Handler\SocketHandler::setSocketTimeout ( )
private

Definition at line 286 of file SocketHandler.php.

287 {
288 if (!$this->streamSetTimeout()) {
289 throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
290 }
291 }
streamSetTimeout()
Wrapper to allow mocking.

References Monolog\Handler\SocketHandler\streamSetTimeout().

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

+ 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.

114 {
115 $this->validateTimeout($seconds);
116 $this->timeout = (float) $seconds;
117 }

References Monolog\Handler\SocketHandler\validateTimeout().

+ 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.

125 {
126 $this->validateTimeout($seconds);
127 $this->writingTimeout = (float) $seconds;
128 }

References Monolog\Handler\SocketHandler\validateTimeout().

+ 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.

234 {
235 return stream_get_meta_data($this->resource);
236 }

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

+ 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.

215 {
216 $seconds = floor($this->timeout);
217 $microseconds = round(($this->timeout - $seconds) * 1e6);
218
219 return stream_set_timeout($this->resource, $seconds, $microseconds);
220 }

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

+ Here is the caller graph for this function:

◆ validateTimeout()

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

Definition at line 238 of file SocketHandler.php.

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 }

References $ok.

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

+ 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

UnexpectedValueException

Exceptions

RuntimeException

Reimplemented from Monolog\Handler\AbstractProcessingHandler.

Reimplemented in Monolog\Handler\FleepHookHandler, Monolog\Handler\FlowdockHandler, Monolog\Handler\HipChatHandler, Monolog\Handler\PushoverHandler, and Monolog\Handler\SlackHandler.

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.

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 }
streamGetMetadata()
Wrapper to allow mocking.

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().

+ 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.

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 }

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

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

+ 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: