Removed Logging

Removed logging - use Decorator as replacement (https://raw.github.com/cboden/RatchetApps/master/lib/RatchetApps/Logger.php)
This commit is contained in:
Chris Boden 2011-11-10 10:49:23 -05:00
parent cf3ba7c4ad
commit bf0787b7cd
10 changed files with 8 additions and 174 deletions

View File

@ -1,25 +0,0 @@
<?php
namespace Ratchet\Logging;
/**
* A logger used by the server and extending applications for debugging info
*/
interface LoggerInterface {
/**
* Just an informational log
* @param string
*/
function note($msg);
/**
* A problem, but nothing too serious
* @param string
*/
function warning($msg);
/**
* Bad things have happened...
* @param string
*/
function error($msg);
}

View File

@ -1,32 +0,0 @@
<?php
namespace Ratchet\Logging;
use Monolog\Logger;
/**
* Adapt the awesome Monolog Logger into the lowly Ratchet Logger
*/
class MonologAdapter extends Logger implements LoggerInterface {
/**
* Maps to Monolog\Logger::addInfo
* @param string
*/
function note($msg) {
$this->addInfo($msg);
}
/**
* Maps to Monolog\Logger::addWarning
* @param string
*/
function warning($msg) {
$this->addWarning($msg);
}
/**
* Maps to Monolog\Logger::addError
* @param string
*/
function error($msg) {
$this->addError($msg);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace Ratchet\Logging;
/**
* Sends all logs into the void
* No one can hear you scream in /dev/null
*/
class NullLogger implements LoggerInterface {
function note($msg) {
}
function warning($msg) {
}
function error($msg) {
}
}

View File

@ -17,6 +17,7 @@ use Ratchet\Protocol\WebSocket\Util\HTTP;
* @todo Learn about closing the socket. A message has to be sent prior to closing - does the message get sent onClose event or CloseConnection command?
* @todo Consider cheating the application...don't call _app::onOpen until handshake is complete - only issue is sending headers/cookies
* @todo Consider chaning this class to a State Pattern. If a SocketObserver is passed in __construct, do what is there now. If it's an AppInterface change behaviour of socket interaction (onOpen, handshake, etc)
* @todo Change namespace to Ratchet\Protocol\WebSocket\Adapter
*/
class WebSocket implements ProtocolInterface {
/**

View File

@ -74,7 +74,7 @@ class HyBi10 implements VersionInterface {
default:
// Close connection on unknown opcode:
throw new \UnexpectedValueException('Unknown opcode');
throw new \UnexpectedValueException("Unknown opcode ({$opcode})");
break;
}

View File

@ -2,8 +2,6 @@
namespace Ratchet;
use Ratchet\Server\Aggregator;
use Ratchet\Protocol\ProtocolInterface;
use Ratchet\Logging\LoggerInterface;
use Ratchet\Logging\NullLogger;
use Ratchet\Command\CommandInterface;
/**
@ -28,11 +26,6 @@ class Server implements SocketObserver, \IteratorAggregate {
*/
protected $_connections;
/**
* @type Logging\LoggerInterface;
*/
protected $_log;
/**
* @var SocketObserver
* Maybe temporary?
@ -42,18 +35,12 @@ class Server implements SocketObserver, \IteratorAggregate {
/**
* @param Ratchet\Socket
* @param SocketObserver
* @param Logging\LoggerInterface
*/
public function __construct(SocketInterface $host, SocketObserver $application, LoggerInterface $logger = null) {
public function __construct(SocketInterface $host, SocketObserver $application) {
$this->_master = $host;
$socket = $host->getResource();
$this->_resources[] = $socket;
if (null === $logger) {
$logger = new NullLogger;
}
$this->_log = $logger;
$this->_connections = new \ArrayIterator(array());
$this->_app = $application;
@ -61,6 +48,7 @@ class Server implements SocketObserver, \IteratorAggregate {
/**
* @return ArrayIterator of SocketInterfaces
* @todo This interface was originally in place as Server was passed up/down chain, but isn't anymore, consider removing
*/
public function getIterator() {
return $this->_connections;
@ -114,7 +102,6 @@ class Server implements SocketObserver, \IteratorAggregate {
}
} catch (Exception $se) {
// Instead of logging error, will probably add/trigger onIOError/onError or something in SocketObserver
$this->_log->error($se->getCode() . ' - ' . $se->getMessage());
// temporary, move to application
if ($se->getCode() != 35) {
@ -122,7 +109,8 @@ class Server implements SocketObserver, \IteratorAggregate {
$close->execute($this);
}
} catch (\Exception $e) {
$this->_log->error('Big uh oh: ' . $e->getMessage());
// onError() - but can I determine which is/was the target Socket that threw the exception...?
// $conn->close() ???
}
} while (true);
}
@ -132,14 +120,10 @@ class Server implements SocketObserver, \IteratorAggregate {
$this->_resources[] = $new_connection->getResource();
$this->_connections[$new_connection->getResource()] = $new_connection;
$this->_log->note('New connection, ' . count($this->_connections) . ' total');
return $this->_app->onOpen($new_connection);
}
public function onRecv(SocketInterface $from, $msg) {
// $this->_log->note('New message "' . trim($msg) . '"');
return $this->_app->onRecv($from, $msg);
}
@ -151,8 +135,6 @@ class Server implements SocketObserver, \IteratorAggregate {
unset($this->_connections[$resource]);
unset($this->_resources[array_search($resource, $this->_resources)]);
$this->_log->note('Connection closed, ' . count($this->_connections) . ' connections remain');
return $cmd;
}
}

View File

@ -5,6 +5,7 @@ namespace Ratchet;
* Observable/Observer design pattern interface for handing events on a socket
* @todo Consider an onException method. Since server is running its own loop the app currently doesn't know when a problem is handled
* @todo Consider an onDisconnect method for a server-side close()'ing of a connection - onClose would be client side close()
* @todo Consider adding __construct(SocketObserver $decorator = null) - on Server move Socket as parameter to run()
*/
interface SocketObserver {
/**

View File

@ -1,30 +0,0 @@
<?php
namespace Ratchet\Tests\Logging;
use Ratchet\Logging\NullLogger;
/**
* @covers Ratchet\Logging\NullLogger
*/
class NullLoggerTest extends \PHPUnit_Framework_TestCase {
protected $_log;
public function setUp() {
$this->_log = new NullLogger;
}
public function testInterface() {
$this->assertInstanceOf('\\Ratchet\\Logging\\LoggerInterface', $this->_log);
}
public function testNoteDoesNothing() {
$this->assertNull($this->_log->note('hi'));
}
public function testWarningDoesNothing() {
$this->assertNull($this->_log->warning('hi'));
}
public function testErrorDoesNothing() {
$this->assertNull($this->_log->error('hi'));
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Logging\LoggerInterface;
class ArrayLogger implements LoggerInterface {
public $last_msg = '';
public function note($msg) {
$this->last_msg = $msg;
}
public function warning($msg) {
$this->last_msg = $msg;
}
public function error($msg) {
$this->last_msg = $msg;
}
}

View File

@ -3,7 +3,6 @@ namespace Ratchet\Tests;
use Ratchet\Server;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Tests\Mock\Application as TestApp;
use Ratchet\Tests\Mock\ArrayLogger;
/**
* @covers Ratchet\Server
@ -32,32 +31,6 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->assertThat($this->_server, $constraint);
}
public function testNullLoggerIsSetInConstruct() {
$this->assertInstanceOf('\\Ratchet\\Logging\\LoggerInterface', $this->getPrivateProperty($this->_server, '_log'));
}
public function testPassedLoggerIsSetInConstruct() {
$logger = new ArrayLogger;
$server = new Server(new Socket(), $this->_app, $logger);
$this->assertSame($logger, $this->getPrivateProperty($server, '_log'));
}
/*
public function testLoggerIsSetInMethod() {
$logger = new ArrayLogger;
$this->_server->setLogger($logger);
$this->assertSame($logger, $this->getPrivateProperty($this->_server, '_log'));
}
*/
/*
public function testGetMasterReturnsCatalyst() {
$this->assertSame($this->_catalyst, $this->_server->getMaster());
}
*/
public function testIteration() {
$this->assertInstanceOf('\\Iterator', $this->_server->getIterator());
}