ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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...
 
 setChunkSize ($bytes)
 Set chunk size. 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...
 
 getChunkSize ()
 Get current chunk size. 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
bool 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
bool
} 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 ()
 
 reset ()
 
 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...
 
 reset ()
 

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...
 
 streamSetChunkSize ()
 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 ()
 
 setStreamChunkSize ()
 
 writeToSocket ($data)
 
 writingIsTimedOut ($sent)
 

Private Attributes

 $connectionString
 
 $connectionTimeout
 
 $resource
 
 $timeout = 0
 
 $writingTimeout = 10
 
 $lastSentBytes = null
 
 $chunkSize = 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
bool$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 41 of file SocketHandler.php.

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

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 66 of file SocketHandler.php.

67 {
68 if (!$this->isPersistent()) {
69 $this->closeSocket();
70 }
71 }
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 76 of file SocketHandler.php.

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

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

+ Here is the caller graph for this function:

◆ connect()

Monolog\Handler\SocketHandler::connect ( )
private

◆ connectIfNotConnected()

Monolog\Handler\SocketHandler::connectIfNotConnected ( )
private

Definition at line 277 of file SocketHandler.php.

278 {
279 if ($this->isConnected()) {
280 return;
281 }
282 $this->connect();
283 }
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 305 of file SocketHandler.php.

306 {
307 if ($this->isPersistent()) {
308 $resource = $this->pfsockopen();
309 } else {
310 $resource = $this->fsockopen();
311 }
312 if (!$resource) {
313 throw new \UnexpectedValueException("Failed connecting to $this->connectionString ($this->errno: $this->errstr)");
314 }
315 $this->resource = $resource;
316 }
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 225 of file SocketHandler.php.

226 {
227 return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
228 }

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 256 of file SocketHandler.php.

257 {
258 return @fwrite($this->resource, $data);
259 }
fwrite($data)
Wrapper to allow mocking.
$data
Definition: bench.php:6

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

◆ getChunkSize()

Monolog\Handler\SocketHandler::getChunkSize ( )

Get current chunk size.

Returns
float

Definition at line 196 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$chunkSize.

◆ getConnectionString()

Monolog\Handler\SocketHandler::getConnectionString ( )

Get current connection string.

Returns
string

Definition at line 146 of file SocketHandler.php.

147 {
149 }

References Monolog\Handler\SocketHandler\$connectionString.

◆ getConnectionTimeout()

Monolog\Handler\SocketHandler::getConnectionTimeout ( )

Get current connection timeout setting.

Returns
float

Definition at line 166 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$connectionTimeout.

◆ getResource()

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

Definition at line 293 of file SocketHandler.php.

294 {
295 return $this->resource;
296 }

References Monolog\Handler\SocketHandler\$resource.

Referenced by Monolog\Handler\HipChatHandler\finalizeWrite(), and Monolog\Handler\SlackHandler\finalizeWrite().

+ Here is the caller graph for this function:

◆ getTimeout()

Monolog\Handler\SocketHandler::getTimeout ( )

Get current in-transfer timeout.

Returns
float

Definition at line 176 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$timeout.

◆ getWritingTimeout()

Monolog\Handler\SocketHandler::getWritingTimeout ( )

Get current local writing timeout.

Returns
float

Definition at line 186 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 208 of file SocketHandler.php.

209 {
210 return is_resource($this->resource)
211 && !feof($this->resource); // on TCP - other party can close connection.
212 }

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 156 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 217 of file SocketHandler.php.

218 {
219 return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
220 }

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:

◆ setChunkSize()

Monolog\Handler\SocketHandler::setChunkSize (   $bytes)

Set chunk size.

Only has effect during connection in the writing cycle.

Parameters
float$bytes

Definition at line 136 of file SocketHandler.php.

137 {
138 $this->chunkSize = $bytes;
139 }

◆ 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 101 of file SocketHandler.php.

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

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 89 of file SocketHandler.php.

90 {
91 $this->persistent = (bool) $persistent;
92 }

References Monolog\Handler\SocketHandler\$persistent.

◆ setSocketTimeout()

Monolog\Handler\SocketHandler::setSocketTimeout ( )
private

Definition at line 318 of file SocketHandler.php.

319 {
320 if (!$this->streamSetTimeout()) {
321 throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
322 }
323 }
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:

◆ setStreamChunkSize()

Monolog\Handler\SocketHandler::setStreamChunkSize ( )
private

Definition at line 325 of file SocketHandler.php.

326 {
327 if ($this->chunkSize && !$this->streamSetChunkSize()) {
328 throw new \UnexpectedValueException("Failed setting chunk size with stream_set_chunk_size()");
329 }
330 }
streamSetChunkSize()
Wrapper to allow mocking.

References Monolog\Handler\SocketHandler\streamSetChunkSize().

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 114 of file SocketHandler.php.

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

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 125 of file SocketHandler.php.

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

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 264 of file SocketHandler.php.

265 {
266 return stream_get_meta_data($this->resource);
267 }

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

+ Here is the caller graph for this function:

◆ streamSetChunkSize()

Monolog\Handler\SocketHandler::streamSetChunkSize ( )
protected

Wrapper to allow mocking.

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

Definition at line 248 of file SocketHandler.php.

249 {
250 return stream_set_chunk_size($this->resource, $this->chunkSize);
251 }

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

+ 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 235 of file SocketHandler.php.

236 {
237 $seconds = floor($this->timeout);
238 $microseconds = round(($this->timeout - $seconds) * 1e6);
239
240 return stream_set_timeout($this->resource, $seconds, $microseconds);
241 }

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

+ Here is the caller graph for this function:

◆ validateTimeout()

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

Definition at line 269 of file SocketHandler.php.

270 {
271 $ok = filter_var($value, FILTER_VALIDATE_FLOAT);
272 if ($ok === false || $value < 0) {
273 throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)");
274 }
275 }

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 56 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 332 of file SocketHandler.php.

333 {
334 $length = strlen($data);
335 $sent = 0;
336 $this->lastSentBytes = $sent;
337 while ($this->isConnected() && $sent < $length) {
338 if (0 == $sent) {
339 $chunk = $this->fwrite($data);
340 } else {
341 $chunk = $this->fwrite(substr($data, $sent));
342 }
343 if ($chunk === false) {
344 throw new \RuntimeException("Could not write to socket");
345 }
346 $sent += $chunk;
347 $socketInfo = $this->streamGetMetadata();
348 if ($socketInfo['timed_out']) {
349 throw new \RuntimeException("Write timed-out");
350 }
351
352 if ($this->writingIsTimedOut($sent)) {
353 throw new \RuntimeException("Write timed-out, no data sent for `{$this->writingTimeout}` seconds, probably we got disconnected (sent $sent of $length)");
354 }
355 }
356 if (!$this->isConnected() && $sent < $length) {
357 throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
358 }
359 }
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 361 of file SocketHandler.php.

362 {
363 $writingTimeout = (int) floor($this->writingTimeout);
364 if (0 === $writingTimeout) {
365 return false;
366 }
367
368 if ($sent !== $this->lastSentBytes) {
369 $this->lastWritingAt = time();
370 $this->lastSentBytes = $sent;
371
372 return false;
373 } else {
374 usleep(100);
375 }
376
377 if ((time() - $this->lastWritingAt) >= $writingTimeout) {
378 $this->closeSocket();
379
380 return true;
381 }
382
383 return false;
384 }

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

◆ $chunkSize

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

Definition at line 30 of file SocketHandler.php.

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

◆ $connectionString

◆ $connectionTimeout

Monolog\Handler\SocketHandler::$connectionTimeout
private

◆ $errno

Monolog\Handler\SocketHandler::$errno
private

Definition at line 32 of file SocketHandler.php.

◆ $errstr

Monolog\Handler\SocketHandler::$errstr
private

Definition at line 33 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 34 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: