Permalink
Browse files

Minor CS fixes, updated changelog

  • Loading branch information...
1 parent 5f40e7e commit b42da84c12055e6f0927a68af8e3baa562126316 @Seldaek committed Apr 22, 2012
Showing with 41 additions and 43 deletions.
  1. +1 −0 CHANGELOG.mdown
  2. +1 −1 doc/sockets.md
  3. +30 −32 src/Monolog/Handler/SocketHandler.php
  4. +9 −10 tests/Monolog/Handler/SocketHandlerTest.php
View
@@ -6,6 +6,7 @@
handle the given log level
* Added ChromePHPHandler
* Added MongoDBHandler
+ * Added SocketHandler (for use with syslog-ng for example)
* Added NormalizerFormatter
* Added the possibility to change the activation strategy of the FingersCrossedHandler
* Added possibility to show microseconds in logs
View
@@ -33,5 +33,5 @@ $logger->addInfo('My logger is now ready');
In this example, using syslog-ng, you should see the log on the log server:
- cweb1 [2012-02-26 00:12:03] my_logger.INFO: My logger is now ready [] []
+ cweb1 [2012-02-26 00:12:03] my_logger.INFO: My logger is now ready [] []
@@ -15,13 +15,12 @@
/**
* 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;
@@ -44,9 +43,9 @@ public function __construct($connectionString, $level = Logger::DEBUG, $bubble =
/**
* Connect (if necessary) and write to the socket
- *
+ *
* @param array $record
- *
+ *
* @throws \UnexpectedValueException
* @throws \RuntimeException
*/
@@ -61,10 +60,9 @@ public function write(array $record)
*/
public function close()
{
- if ($this->isPersistent()) {
- return;
+ if (!$this->isPersistent()) {
+ $this->closeSocket();
}
- $this->closeSocket();
}
/**
@@ -80,8 +78,8 @@ public function closeSocket()
/**
* Set socket connection to nbe persistent. It only has effect before the connection is initiated.
- *
- * @param type $boolean
+ *
+ * @param type $boolean
*/
public function setPersistent($boolean)
{
@@ -90,9 +88,9 @@ public function setPersistent($boolean)
/**
* Set connection timeout. Only has effect before we connect.
- *
- * @param integer $seconds
- *
+ *
+ * @param integer $seconds
+ *
* @see http://php.net/manual/en/function.fsockopen.php
*/
public function setConnectionTimeout($seconds)
@@ -103,9 +101,9 @@ public function setConnectionTimeout($seconds)
/**
* Set write timeout. Only has effect before we connect.
- *
- * @param type $seconds
- *
+ *
+ * @param type $seconds
+ *
* @see http://php.net/manual/en/function.stream-set-timeout.php
*/
public function setTimeout($seconds)
@@ -116,7 +114,7 @@ public function setTimeout($seconds)
/**
* Get current connection string
- *
+ *
* @return string
*/
public function getConnectionString()
@@ -126,7 +124,7 @@ public function getConnectionString()
/**
* Get persistent setting
- *
+ *
* @return boolean
*/
public function isPersistent()
@@ -136,7 +134,7 @@ public function isPersistent()
/**
* Get current connection timeout setting
- *
+ *
* @return float
*/
public function getConnectionTimeout()
@@ -146,7 +144,7 @@ public function getConnectionTimeout()
/**
* Get current in-transfer timeout
- *
+ *
* @return float
*/
public function getTimeout()
@@ -156,53 +154,53 @@ public function getTimeout()
/**
* 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.
+ && !feof($this->resource); // on TCP - other party can close connection.
}
-
+
/**
- * Allow mock
+ * Wrapper to allow mocking
*/
protected function pfsockopen()
{
return @pfsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
}
/**
- * Allow mock
+ * Wrapper to allow mocking
*/
protected function fsockopen()
{
return @fsockopen($this->connectionString, -1, $this->errno, $this->errstr, $this->connectionTimeout);
}
/**
- * Allow mock
+ * Wrapper to allow mocking
*/
- protected function stream_set_timeout()
+ protected function streamSetTimeout()
{
return stream_set_timeout($this->resource, $this->timeout);
}
/**
- * Allow mock
+ * Wrapper to allow mocking
*/
protected function fwrite($data)
{
return @fwrite($this->resource, $data);
}
/**
- * Allow mock
+ * Wrapper to allow mocking
*/
- protected function stream_get_meta_data()
+ protected function streamGetMetadata()
{
return stream_get_meta_data($this->resource);
}
@@ -246,7 +244,7 @@ private function createSocketResource()
private function setSocketTimeout()
{
- if (!$this->stream_set_timeout()) {
+ if (!$this->streamSetTimeout()) {
throw new \UnexpectedValueException("Failed setting timeout with stream_set_timeout()");
}
}
@@ -261,7 +259,7 @@ private function writeToSocket($data)
throw new \RuntimeException("Could not write to socket");
}
$sent += $chunk;
- $socketInfo = $this->stream_get_meta_data();
+ $socketInfo = $this->streamGetMetadata();
if ($socketInfo['timed_out']) {
throw new \RuntimeException("Write timed-out");
}
@@ -19,7 +19,6 @@
*/
class SocketHandlerTest extends TestCase
{
-
/**
* @var Monolog\Handler\SocketHandler
*/
@@ -107,9 +106,9 @@ public function testExceptionIsThrownOnPfsockopenError()
*/
public function testExceptionIsThrownIfCannotSetTimeout()
{
- $this->setMockHandler(array('stream_set_timeout'));
+ $this->setMockHandler(array('streamSetTimeout'));
$this->handler->expects($this->once())
- ->method('stream_set_timeout')
+ ->method('streamSetTimeout')
->will($this->returnValue(false));
$this->writeRecord('Hello world');
}
@@ -142,7 +141,7 @@ public function testWriteFailsOnIfFwriteReturnsFalse()
*/
public function testWriteFailsIfStreamTimesOut()
{
- $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
+ $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
$callback = function($arg)
{
@@ -157,7 +156,7 @@ public function testWriteFailsIfStreamTimesOut()
->method('fwrite')
->will($this->returnCallback($callback));
$this->handler->expects($this->exactly(1))
- ->method('stream_get_meta_data')
+ ->method('streamGetMetadata')
->will($this->returnValue(array('timed_out' => true)));
@@ -169,7 +168,7 @@ public function testWriteFailsIfStreamTimesOut()
*/
public function testWriteFailsOnIncompleteWrite()
{
- $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
+ $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
$res = $this->res;
$callback = function($string) use ($res)
@@ -182,7 +181,7 @@ public function testWriteFailsOnIncompleteWrite()
->method('fwrite')
->will($this->returnCallback($callback));
$this->handler->expects($this->exactly(1))
- ->method('stream_get_meta_data')
+ ->method('streamGetMetadata')
->will($this->returnValue(array('timed_out' => false)));
$this->writeRecord('Hello world');
@@ -252,7 +251,7 @@ private function setMockHandler(array $methods = array())
{
$this->res = fopen('php://memory', 'a');
- $defaultMethods = array('fsockopen', 'pfsockopen', 'stream_set_timeout');
+ $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
$newMethods = array_diff($methods, $defaultMethods);
$finalMethods = array_merge($defaultMethods, $newMethods);
@@ -273,9 +272,9 @@ private function setMockHandler(array $methods = array())
->will($this->returnValue($this->res));
}
- if (!in_array('stream_set_timeout', $methods)) {
+ if (!in_array('streamSetTimeout', $methods)) {
$this->handler->expects($this->any())
- ->method('stream_set_timeout')
+ ->method('streamSetTimeout')
->will($this->returnValue(true));
}

0 comments on commit b42da84

Please sign in to comment.