Skip to content

Commit

Permalink
Added server logging
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstevenson committed Sep 26, 2012
1 parent 75729a2 commit b568ec3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ Json-Rpc is licensed under the MIT License - see the `LICENSE` file for details
[composer]: http://getcomposer.org
[download]: https://github.com/johnstevenson/json-rpc/downloads
[wiki]:https://github.com/johnstevenson/json-rpc/wiki/Home
[client]:https://github.com/johnstevenson/json-rpc/wiki/Client-Usage
[server]:https://github.com/johnstevenson/json-rpc/wiki/Server-Usage
[advanced]:https://github.com/johnstevenson/json-rpc/wiki/Advanced-Functionality
[client]:https://github.com/johnstevenson/json-rpc/wiki/Client-usage
[server]:https://github.com/johnstevenson/json-rpc/wiki/Server-usage
[advanced]:https://github.com/johnstevenson/json-rpc/wiki/Advanced-functionality
9 changes: 8 additions & 1 deletion src/JsonRpc/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Client
private $requests = array();
private $multi = false;
private $notifications = 0;
private $headers = array();


const ERR_RPC_RESPONSE = 'Invalid Response';
Expand All @@ -78,6 +79,12 @@ public function __construct($url, $transport = null)
}


public function setHeader($header)
{
$this->headers[] = $header;
}


public function setTransport($transport)
{
$this->transport = $transport;
Expand Down Expand Up @@ -169,7 +176,7 @@ private function send()
try
{

if ($res = $this->transport->send('POST', $this->url, $data))
if ($res = $this->transport->send('POST', $this->url, $data, $this->headers))
{
$this->output = $this->transport->output;
$res = $this->checkResult();
Expand Down
14 changes: 5 additions & 9 deletions src/JsonRpc/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ class Server
private $refClass = null;


public function __construct($methodHandler, $otherHandlers = array())
public function __construct($methodHandler, $transport = null)
{

ini_set('display_errors', '0');

$this->handler = $methodHandler;

$other = (array) $otherHandlers;

$this->transport = !empty($other['transport']) ? $other['transport'] : null;
$this->logger = !empty($other['logger']) ? $other['logger'] : null;
$this->transport = $transport;

if (!$this->transport)
{
Expand Down Expand Up @@ -388,21 +384,21 @@ private function logError($message)

$params = array(
500,
$msg
$message
);

$result = call_user_func_array($callback, $params);

}
else
{
error_log($msg);
error_log($message);
}

}
catch (\Exception $e)
{
error_log($msg);
error_log($e->__toString());
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/JsonRpc/Transport/BasicServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public function receive()
}


public function reply($json)
public function reply($data)
{
echo $json;
echo $data;
exit;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/ServerTests/ServerErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,26 @@ public function testNotifyWrongMethod()
$this->assertEquals($expects, $json);
}

public function testExceptionWithLogger()
{

$this->methods = 'MethodsException';
parent::setUp();

$logger = new ServerLogger();
$this->server->setLogger($logger);

$data = '{"jsonrpc": "2.0", "method": "divide", "params": [42, 0], "id": 1}';
$expects = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "Internal error"}, "id": 1}';
$json = $this->getResponseJson($data, $expects);
$this->assertEquals($expects, $json);

$expects = 500;
$this->assertEquals($expects, $logger->level);

$expects = 'divide';
$this->assertContains($expects, $logger->message);

}

}
31 changes: 31 additions & 0 deletions tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ public static function ping($msg, $user)

}

class MethodsException
{

public static function divide($dividend, $divisor, $int = false)
{
throw new Exception(__FUNCTION__);
}

public static function ping($msg, $user)
{
throw new Exception(__FUNCTION__);
}

}

class ServerLogger
{

public $level;
public $message;

public function addRecord($level, $message, array $context = array())
{
$this->level = $level;
$this->message = $message;
}

}



/**
* Dummy functions for testing
*/
Expand Down

0 comments on commit b568ec3

Please sign in to comment.