ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 ($boolean)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write 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...
 
 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)
 
- 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)
 

Private Attributes

 $connectionString
 
 $connectionTimeout
 
 $resource
 
 $timeout = 0
 
 $persistent = false
 
 $errno
 
 $errstr
 

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
integer$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 37 of file SocketHandler.php.

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

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

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

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

63  {
64  if (!$this->isPersistent()) {
65  $this->closeSocket();
66  }
67  }
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 72 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(), and Monolog\Handler\SlackHandler\write().

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

◆ connect()

Monolog\Handler\SocketHandler::connect ( )
private

Definition at line 235 of file SocketHandler.php.

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

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

236  {
237  $this->createSocketResource();
238  $this->setSocketTimeout();
239  }
+ 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 222 of file SocketHandler.php.

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

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

223  {
224  if ($this->isConnected()) {
225  return;
226  }
227  $this->connect();
228  }
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 241 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().

242  {
243  if ($this->isPersistent()) {
244  $resource = $this->pfsockopen();
245  } else {
246  $resource = $this->fsockopen();
247  }
248  if (!$resource) {
249  throw new \UnexpectedValueException("Failed connecting to $this->connectionString ($this->errno: $this->errstr)");
250  }
251  $this->resource = $resource;
252  }
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 180 of file SocketHandler.php.

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

181  {
182  return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
183  }
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 201 of file SocketHandler.php.

References $data.

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

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

◆ generateDataStream()

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

Definition at line 230 of file SocketHandler.php.

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

231  {
232  return (string) $record['formatted'];
233  }
+ Here is the caller graph for this function:

◆ getConnectionString()

Monolog\Handler\SocketHandler::getConnectionString ( )

Get current connection string.

Returns
string

Definition at line 121 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$connectionString.

122  {
124  }

◆ getConnectionTimeout()

Monolog\Handler\SocketHandler::getConnectionTimeout ( )

Get current connection timeout setting.

Returns
float

Definition at line 141 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$connectionTimeout.

142  {
144  }

◆ getTimeout()

Monolog\Handler\SocketHandler::getTimeout ( )

Get current in-transfer timeout.

Returns
float

Definition at line 151 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$timeout.

152  {
153  return $this->timeout;
154  }

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

Definition at line 163 of file SocketHandler.php.

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

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

◆ isPersistent()

Monolog\Handler\SocketHandler::isPersistent ( )

Get persistent setting.

Returns
boolean

Definition at line 131 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\$persistent.

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

132  {
133  return $this->persistent;
134  }
+ Here is the caller graph for this function:

◆ pfsockopen()

Monolog\Handler\SocketHandler::pfsockopen ( )
protected

Wrapper to allow mocking.

Definition at line 172 of file SocketHandler.php.

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

173  {
174  return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
175  }
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 97 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\validateTimeout().

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

◆ setPersistent()

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

Set socket connection to nbe persistent.

It only has effect before the connection is initiated.

Parameters
type$boolean

Definition at line 85 of file SocketHandler.php.

86  {
87  $this->persistent = (boolean) $boolean;
88  }

◆ setSocketTimeout()

Monolog\Handler\SocketHandler::setSocketTimeout ( )
private

Definition at line 254 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\streamSetTimeout().

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

255  {
256  if (!$this->streamSetTimeout()) {
257  throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
258  }
259  }
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 110 of file SocketHandler.php.

References Monolog\Handler\SocketHandler\validateTimeout().

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

◆ streamGetMetadata()

Monolog\Handler\SocketHandler::streamGetMetadata ( )
protected

Wrapper to allow mocking.

Definition at line 209 of file SocketHandler.php.

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

210  {
211  return stream_get_meta_data($this->resource);
212  }
+ 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 190 of file SocketHandler.php.

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

191  {
192  $seconds = floor($this->timeout);
193  $microseconds = round(($this->timeout - $seconds)*1e6);
194 
195  return stream_set_timeout($this->resource, $seconds, $microseconds);
196  }
+ Here is the caller graph for this function:

◆ validateTimeout()

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

Definition at line 214 of file SocketHandler.php.

References $ok.

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

215  {
216  $ok = filter_var($value, FILTER_VALIDATE_FLOAT);
217  if ($ok === false || $value < 0) {
218  throw new \InvalidArgumentException("Timeout must be 0 or a positive float (got $value)");
219  }
220  }
+ 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 52 of file SocketHandler.php.

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

53  {
54  $this->connectIfNotConnected();
55  $data = $this->generateDataStream($record);
56  $this->writeToSocket($data);
57  }
$data
+ Here is the call graph for this function:

◆ writeToSocket()

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

Definition at line 261 of file SocketHandler.php.

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

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

262  {
263  $length = strlen($data);
264  $sent = 0;
265  while ($this->isConnected() && $sent < $length) {
266  if (0 == $sent) {
267  $chunk = $this->fwrite($data);
268  } else {
269  $chunk = $this->fwrite(substr($data, $sent));
270  }
271  if ($chunk === false) {
272  throw new \RuntimeException("Could not write to socket");
273  }
274  $sent += $chunk;
275  $socketInfo = $this->streamGetMetadata();
276  if ($socketInfo['timed_out']) {
277  throw new \RuntimeException("Write timed-out");
278  }
279  }
280  if (!$this->isConnected() && $sent < $length) {
281  throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
282  }
283  }
fwrite($data)
Wrapper to allow mocking.
isConnected()
Check to see if the socket is currently available.
$data
streamGetMetadata()
Wrapper to allow mocking.
+ 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 29 of file SocketHandler.php.

◆ $errstr

Monolog\Handler\SocketHandler::$errstr
private

Definition at line 30 of file SocketHandler.php.

◆ $persistent

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

Definition at line 28 of file SocketHandler.php.

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

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


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