Skip to content

Commit

Permalink
Use throwable instead of exception (#31)
Browse files Browse the repository at this point in the history
* Use throwable instead of exception

* Use correct namespace

* Remove superfloous typehinting

* Use FQCN for Throwable

* Added changelog
  • Loading branch information
gmponos authored Jul 5, 2020
1 parent 64a67a6 commit 8f355d9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 56 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

All Notable changes to `gmponos/guzzle_logger` will be documented in this file

## 2.0.0 - 2020-07-05

### Changed
- [BC] Changed the signature of `HandlerInterface::log` to allow Throwables. Now the signature is
```php
HandlerInterface::log(
LoggerInterface $logger,
RequestInterface $request,
?ResponseInterface $response = null,
?Throwable $exception = null,
?TransferStats $stats = null,
array $options = []
)
```

## 1.1.0 - 2019-09-03

### Added
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ final class SimpleHandler implements HandlerInterface
public function log(
LoggerInterface $logger,
RequestInterface $request,
?ResponseInterface $response,
?\Exception $exception,
?TransferStats $stats,
array $options
?ResponseInterface $response = null,
?\Throwable $exception = null,
?TransferStats $stats = null,
array $options = []
): void {
$logger->debug('Guzzle HTTP request: ' . \GuzzleHttp\Psr7\str($request));
return;
Expand Down
12 changes: 6 additions & 6 deletions src/Handler/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace GuzzleLogMiddleware\Handler;

use Exception;
use GuzzleHttp\TransferStats;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;

/**
* Classes that will implement this interface are responsible
* to log the MessageInterface|\Exception|TransferStats that are
* to log the MessageInterface|\Throwable|TransferStats that are
* passed as values.
*
* @author George Mponos <gmponos@gmail.com>
Expand All @@ -22,9 +22,9 @@ interface HandlerInterface
public function log(
LoggerInterface $logger,
RequestInterface $request,
?ResponseInterface $response,
?Exception $exception,
?TransferStats $stats,
array $options
?ResponseInterface $response = null,
?Throwable $exception = null,
?TransferStats $stats = null,
array $options = []
): void;
}
5 changes: 2 additions & 3 deletions src/Handler/LogLevelStrategy/FixedStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ public function __construct(
/**
* Returns the log level.
*
* @param MessageInterface|\Exception|TransferStats $value
* @param array $options
* @param MessageInterface|\Throwable|TransferStats $value
*/
public function getLevel($value, array $options): string
{
if ($value instanceof RequestException) {
return $this->defaultLevel;
}

if ($value instanceof \Exception) {
if ($value instanceof \Throwable) {
return $this->exceptionLevel;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Handler/LogLevelStrategy/LogLevelStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ interface LogLevelStrategyInterface
/**
* Returns the log level.
*
* @param MessageInterface|\Exception|TransferStats $value
* @param array $options
* @return string The LogLevel
* @param MessageInterface|\Throwable|TransferStats $value
*/
public function getLevel($value, array $options): string;
}
4 changes: 1 addition & 3 deletions src/Handler/LogLevelStrategy/StatusCodeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace GuzzleLogMiddleware\Handler\LogLevelStrategy;

use GuzzleHttp\TransferStats;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LogLevel;

Expand Down Expand Up @@ -53,7 +51,7 @@ public function setLevel(int $statusCode, string $level): void
public function getLevel($value, array $options): string
{
$this->setOptions($options);
if ($value instanceof \Exception) {
if ($value instanceof \Throwable) {
return $this->exceptionLevel;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Handler/LogLevelStrategy/ThresholdStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ final class ThresholdStrategy implements LogLevelStrategyInterface
*
* @param array $thresholds An array of thresholds.
* @param string $defaultLevel The that will be used for the requests and as a default one.
* @param string $exceptionLevel
*/
public function __construct(
array $thresholds = [],
Expand All @@ -71,7 +70,7 @@ public function __construct(

public function getLevel($value, array $options): string
{
if ($value instanceof \Exception) {
if ($value instanceof \Throwable) {
return $this->exceptionLevel;
}

Expand Down
40 changes: 11 additions & 29 deletions src/Handler/MultiRecordArrayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace GuzzleLogMiddleware\Handler;

use Exception;
use GuzzleHttp\TransferStats;
use GuzzleLogMiddleware\Handler\LogLevelStrategy\LogLevelStrategyInterface;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;

/**
* @author George Mponos <gmponos@gmail.com>
Expand All @@ -28,12 +28,14 @@ final class MultiRecordArrayHandler extends AbstractHandler
private $summarySize;

/**
* @param LogLevelStrategyInterface|null $logLevelStrategy
* @param int $truncateSize If the body of the request/response is greater than the size of this integer the body will be truncated
* @param int $summarySize The size to use for the summary of a truncated body
*/
public function __construct(LogLevelStrategyInterface $logLevelStrategy = null, int $truncateSize = 3500, int $summarySize = 200)
{
public function __construct(
LogLevelStrategyInterface $logLevelStrategy = null,
int $truncateSize = 3500,
int $summarySize = 200
) {
$this->logLevelStrategy = $logLevelStrategy === null ? $this->getDefaultStrategy() : $logLevelStrategy;
$this->truncateSize = $truncateSize;
$this->summarySize = $summarySize;
Expand All @@ -42,10 +44,10 @@ public function __construct(LogLevelStrategyInterface $logLevelStrategy = null,
public function log(
LoggerInterface $logger,
RequestInterface $request,
?ResponseInterface $response,
?Exception $exception,
?TransferStats $stats,
array $options
?ResponseInterface $response = null,
?Throwable $exception = null,
?TransferStats $stats = null,
array $options = []
): void {
$this->logRequest($logger, $request, $options);

Expand Down Expand Up @@ -75,12 +77,6 @@ private function logRequest(LoggerInterface $logger, RequestInterface $request,
$logger->log($level, 'Guzzle HTTP request', $context);
}

/**
* @param LoggerInterface $logger
* @param ResponseInterface $response
* @param array $options
* @return void
*/
private function logResponse(LoggerInterface $logger, ResponseInterface $response, array $options): void
{
$context['response']['headers'] = $response->getHeaders();
Expand All @@ -96,13 +92,7 @@ private function logResponse(LoggerInterface $logger, ResponseInterface $respons
$logger->log($level, 'Guzzle HTTP response', $context);
}

/**
* @param LoggerInterface $logger
* @param Exception|null $exception
* @param array $options
* @return void
*/
private function logReason(LoggerInterface $logger, ?Exception $exception, array $options): void
private function logReason(LoggerInterface $logger, ?Throwable $exception, array $options): void
{
if ($exception === null) {
return;
Expand All @@ -117,12 +107,6 @@ private function logReason(LoggerInterface $logger, ?Exception $exception, array
$logger->log($level, 'Guzzle HTTP exception', $context);
}

/**
* @param LoggerInterface $logger
* @param TransferStats $stats
* @param array $options
* @return void
*/
private function logStats(LoggerInterface $logger, TransferStats $stats, array $options): void
{
$this->logLevelStrategy->getLevel($stats, $options);
Expand All @@ -133,8 +117,6 @@ private function logStats(LoggerInterface $logger, TransferStats $stats, array $
}

/**
* @param MessageInterface $message
* @param array $options
* @return string|array
*/
private function formatBody(MessageInterface $message, array $options)
Expand Down
12 changes: 6 additions & 6 deletions src/Handler/StringHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace GuzzleLogMiddleware\Handler;

use Exception;
use GuzzleHttp\TransferStats;
use GuzzleLogMiddleware\Handler\LogLevelStrategy\LogLevelStrategyInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;

/**
* @author George Mponos <gmponos@gmail.com>
Expand All @@ -24,10 +24,10 @@ public function __construct(LogLevelStrategyInterface $logLevelStrategy = null)
public function log(
LoggerInterface $logger,
RequestInterface $request,
?ResponseInterface $response,
?Exception $exception,
?TransferStats $stats,
array $options
?ResponseInterface $response = null,
?Throwable $exception = null,
?TransferStats $stats = null,
array $options = []
): void {
$this->logRequest($logger, $request, $options);

Expand Down Expand Up @@ -73,7 +73,7 @@ private function logResponse(LoggerInterface $logger, ResponseInterface $value,
$logger->log($level, 'Guzzle HTTP response:' . "\n" . $str);
}

private function logReason(LoggerInterface $logger, Exception $exception, array $options): void
private function logReason(LoggerInterface $logger, Throwable $exception, array $options): void
{
$level = $this->logLevelStrategy->getLevel($exception, $options);
$logger->log($level, sprintf('Guzzle HTTP exception: %s', $exception->getMessage()), [
Expand Down

0 comments on commit 8f355d9

Please sign in to comment.