Skip to content

Commit

Permalink
ADD PSR-3 exception error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
srjlewis committed Oct 31, 2024
1 parent 90a8f44 commit 367d798
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 139 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Features
- Authentication and IP based client restrictions
- Custom Middleware
- Fully unit tested
- Requirements: PHP >= 7.1|8
- Requirements: PHP >= 8.0
- License: MIT

Author
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"fguillot/json-rpc": "1.*"
},
"require": {
"php": ">=7.1",
"ext-json": "*"
"php": ">=8.0",
"ext-json": "*",
"psr/log": ">=3"
},
"autoload": {
"psr-0": {"JsonRPC": "src/"}
},
"require-dev": {
"phpunit/phpunit": "^7"
"phpunit/phpunit": "^9 || ^10.0 || ^11.0"
}
}
15 changes: 0 additions & 15 deletions src/JsonRPC/Logger/NullLogger.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/JsonRPC/Request/BatchRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function parse()
->withProcedureHandler($this->procedureHandler)
->withMiddlewareHandler($this->middlewareHandler)
->withLocalException($this->localExceptions)
->withLogger($this->logger)
->withRequestLogger($this->requestLogger)
->parse();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php

namespace JsonRPC\Logger;
namespace JsonRPC\Request\Logger;

/**
* Created by PhpStorm.
* User: StevenLewis
* Date: 09/05/2017
* Time: 12:30
*/
class DebugLogger implements LoggerInterface
class DebugRequestLogger implements RequestLoggerInterface
{

/**
* recorded requests
* @var array
*/
protected $logs = array();
protected array $logs = array();

public function log($id, $method, $params, $response, $timeTaken = 0, $metadata = array())
public function log($id, $method, $params, $response, int $timeTaken = 0, array $metadata = array())
{
$this->logs[] = array(
'id' => $id,
Expand All @@ -28,7 +29,7 @@ public function log($id, $method, $params, $response, $timeTaken = 0, $metadata
);
}

public function getLogs()
public function getLogs(): array
{
return $this->logs;
}
Expand Down
16 changes: 16 additions & 0 deletions src/JsonRPC/Request/Logger/NullRequestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace JsonRPC\Request\Logger;

/**
* Created by PhpStorm.
* User: StevenLewis
* Date: 09/05/2017
* Time: 12:29
*/
class NullRequestLogger implements RequestLoggerInterface
{

public function log($id, $method, $params, $response, int $timeTaken = 0, array $metadata = array())
{
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace JsonRPC\Logger;
namespace JsonRPC\Request\Logger;
/**
* Created by PhpStorm.
* User: StevenLewis
* Date: 09/05/2017
* Time: 12:27
*/
interface LoggerInterface
interface RequestLoggerInterface
{
/**
* Record a request
Expand All @@ -19,5 +19,5 @@ interface LoggerInterface
* @param int $timeTaken
* @param array $metadata
*/
public function log($id, $method, $params, $response, $timeTaken = 0, $metadata = array());
public function log($id, $method, $params, $response, int $timeTaken = 0, array $metadata = []);
}
32 changes: 23 additions & 9 deletions src/JsonRPC/Request/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use JsonRPC\Exception\AccessDeniedException;
use JsonRPC\Exception\AuthenticationFailureException;
use JsonRPC\Exception\InvalidJsonRpcFormatException;
use JsonRPC\Logger\LoggerInterface;
use JsonRPC\MiddlewareHandler;
use JsonRPC\ProcedureHandler;
use JsonRPC\Request\Logger\RequestLoggerInterface;
use JsonRPC\Response\ResponseBuilder;
use JsonRPC\Validator\JsonFormatValidator;
use JsonRPC\Validator\RpcFormatValidator;
use Psr\Log\LoggerInterface;

/**
* Class RequestParser
Expand Down Expand Up @@ -60,9 +61,11 @@ class RequestParser
* Server call logger
*
* @access protected
* @var LoggerInterface
* @var RequestLoggerInterface
*/
protected $logger;
protected $requestLogger;

protected ?LoggerInterface $psr3Logger;

/**
* Get new object instance
Expand Down Expand Up @@ -133,13 +136,19 @@ public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler)
return $this;
}

public function withPsr3Logger(?LoggerInterface $logger): static
{
$this->psr3Logger = $logger;
return $this;
}

/**
* @param LoggerInterface $logger
* @param RequestLoggerInterface $logger
* @return $this
*/
public function withLogger(LoggerInterface $logger)
public function withRequestLogger(RequestLoggerInterface $logger)
{
$this->logger = $logger;
$this->requestLogger = $logger;
return $this;
}

Expand Down Expand Up @@ -182,8 +191,8 @@ public function parse()
$endTime = round(microtime(true),5, PHP_ROUND_HALF_UP);
$callTime = round(($endTime - $startTime),5, PHP_ROUND_HALF_UP);

if($this->logger){
$this->logger->log(
if($this->requestLogger){
$this->requestLogger->log(
(empty($this->payload['id']) ? 0 : $this->payload['id']),
(empty($this->payload['method']) ? '' : $this->payload['method']),
(empty($this->payload['params']) ? array() : $this->payload['params']),
Expand Down Expand Up @@ -213,9 +222,14 @@ protected function handleExceptions(Exception $e)

if ($e instanceof InvalidJsonRpcFormatException || ! $this->isNotification()) {
return ResponseBuilder::create()
->withId(isset($this->payload['id']) ? $this->payload['id'] : null)
->withPsr3Logger($this->psr3Logger)
->withId($this->payload['id'] ?? null)
->withException($e)
->build();
} elseif($this->psr3Logger) {
$this->psr3Logger->error(
$e->getMessage(), ['file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace()]
);
}

return '';
Expand Down
Loading

0 comments on commit 367d798

Please sign in to comment.