| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Monolog\Handler;
- use Monolog\Logger;
- /**
- * Stores to any socket - uses fsockopen() or pfsockopen().
- *
- * @author Pablo de Leon Belloc <pablolb@gmail.com>
- * @see http://php.net/manual/en/function.fsockopen.php
- */
- class SocketHandler extends AbstractProcessingHandler
- {
- private $connectionString;
- private $connectionTimeout;
- private $resource;
- private $timeout = 0;
- private $persistent = false;
- private $errno;
- private $errstr;
- /**
- * @param string $connectionString
- * @param integer $level The minimum logging level at which this handler will be triggered
- * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
- */
- public function __construct($connectionString, $level = Logger::DEBUG, $bubble = true)
- {
- parent::__construct($level, $bubble);
- $this->connectionString = $connectionString;
- $this->connectionTimeout = (float) ini_get('default_socket_timeout');
- }
- /**
- * Connect (if necessary) and write to the socket
- *
- * @throws \UnexpectedValueException
- * @throws \RuntimeException
- * @param string $string
- */
- public function write(array $record)
- {
- $this->connectIfNotConnected();
- $this->writeToSocket((string) $record['formatted']);
- }
- /**
- * We will not close a PersistentSocket instance so it can be reused in other requests.
- */
- public function close()
- {
- if ($this->isPersistent()) {
- return;
- }
- $this->closeSocket();
- }
- public function closeSocket()
- {
- if (is_resource($this->resource)) {
- fclose($this->resource);
- $this->resource = null;
- }
- }
- public function setPersistent($boolean)
- {
- $this->persistent = (boolean) $boolean;
- }
- /**
- * Set connection timeout. Only has effect before we connect.
- *
- * @see http://php.net/manual/en/function.fsockopen.php
- * @param integer $seconds
- */
- public function setConnectionTimeout($seconds)
- {
- $this->validateTimeout($seconds);
- $this->connectionTimeout = (float) $seconds;
- }
- /**
- * Set write timeout. Only has effect before we connect.
- *
- * @see http://php.net/manual/en/function.stream-set-timeout.php
- * @param type $seconds
- */
- public function setTimeout($seconds)
- {
- $this->validateTimeout($seconds);
- $this->timeout = (int) $seconds;
- }
- private function validateTimeout($value)
- {
- $ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array(
- 'min_range' => 0,
- )));
- if ($ok === false) {
- throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)");
- }
- }
- public function getConnectionString()
- {
- return $this->connectionString;
- }
- public function isPersistent()
- {
- return $this->persistent;
- }
- public function getConnectionTimeout()
- {
- return $this->connectionTimeout;
- }
- public function getTimeout()
- {
- return $this->timeout;
- }
- private function connectIfNotConnected()
- {
- if ($this->isConnected()) {
- return;
- }
- $this->connect();
- }
- /**
- * 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.
- *
- * @return boolean
- */
- public function isConnected()
- {
- return is_resource($this->resource)
- && !feof($this->resource); // on TCP - other party can close connection.
- }
- private function connect()
- {
- $this->createSocketResource();
- $this->setSocketTimeout();
- }
- private function createSocketResource()
- {
- if ($this->isPersistent()) {
- $resource = $this->pfsockopen();
- } else {
- $resource = $this->fsockopen();
- }
- if (!$resource) {
- throw new \UnexpectedValueException("Failed connecting to $this->connectionString ($this->errno: $this->errstr)");
- }
- return $this->resource = $resource;
- }
- private function setSocketTimeout()
- {
- if (!$this->stream_set_timeout()) {
- throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
- }
- }
- private function writeToSocket($data)
- {
- $length = strlen($data);
- $sent = 0;
- while ($this->isConnected() && $sent < $length) {
- $chunk = $this->fwrite(substr($data, $sent));
- if ($chunk === false) {
- throw new \RuntimeException("Could not write to socket");
- }
- $sent += $chunk;
- $socketInfo = $this->stream_get_meta_data();
- if ($socketInfo['timed_out']) {
- throw new \RuntimeException("Write timed-out");
- }
- }
- if (!$this->isConnected() && $sent < $length) {
- throw new \RuntimeException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
- }
- }
- /**
- * Allow mock
- */
- protected function pfsockopen()
- {
- return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
- }
- /**
- * Allow mock
- */
- protected function fsockopen()
- {
- return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
- }
- /**
- * Allow mock
- */
- protected function stream_set_timeout()
- {
- return stream_set_timeout($this->resource, $this->timeout);
- }
- /**
- * Allow mock
- */
- protected function fwrite($data)
- {
- return @fwrite($this->resource, $data);
- }
- /**
- * Allow mock
- */
- protected function stream_get_meta_data()
- {
- return stream_get_meta_data($this->resource);
- }
- }
|