diff --git a/spec/LoggerPluginSpec.php b/spec/LoggerPluginSpec.php index a1b8e6a..115ed27 100644 --- a/spec/LoggerPluginSpec.php +++ b/spec/LoggerPluginSpec.php @@ -39,8 +39,8 @@ function it_logs_request_and_response( $formatter->formatRequest($request)->willReturn('GET / 1.1'); $formatter->formatResponse($response)->willReturn('200 OK 1.1'); - $logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled(); - $logger->info('Receive response: "200 OK 1.1" for request: "GET / 1.1"', ['request' => $request, 'response' => $response])->shouldBeCalled(); + $logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled(); + $logger->info("Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1", ['request' => $request, 'response' => $response])->shouldBeCalled(); $next = function () use ($response) { return new FulfilledPromise($response->getWrappedObject()); @@ -55,8 +55,8 @@ function it_logs_exception(LoggerInterface $logger, Formatter $formatter, Reques $exception = new NetworkException('Cannot connect', $request->getWrappedObject()); - $logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled(); - $logger->error('Error: "Cannot connect" when emitting request: "GET / 1.1"', ['request' => $request, 'exception' => $exception])->shouldBeCalled(); + $logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled(); + $logger->error("Error:\nCannot connect\nwhen sending request:\nGET / 1.1", ['request' => $request, 'exception' => $exception])->shouldBeCalled(); $next = function () use ($exception) { return new RejectedPromise($exception); @@ -76,8 +76,8 @@ function it_logs_response_within_exception( $exception = new HttpException('Forbidden', $request->getWrappedObject(), $response->getWrappedObject()); - $logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled(); - $logger->error('Error: "Forbidden" with response: "403 Forbidden 1.1" when emitting request: "GET / 1.1"', [ + $logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled(); + $logger->error("Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1", [ 'request' => $request, 'response' => $response, 'exception' => $exception diff --git a/src/LoggerPlugin.php b/src/LoggerPlugin.php index a09dcdc..b8f9fde 100644 --- a/src/LoggerPlugin.php +++ b/src/LoggerPlugin.php @@ -11,29 +11,15 @@ use Psr\Log\LoggerInterface; /** - * Log request, response and exception for a HTTP Client. + * Log request, response and exception for an HTTP Client. * * @author Joel Wurtz */ final class LoggerPlugin implements Plugin { - /** - * Logger to log request / response / exception for a http call. - * - * @var LoggerInterface - */ private $logger; - - /** - * Formats a request/response as string. - * - * @var Formatter - */ private $formatter; - /** - * @param LoggerInterface $logger - */ public function __construct(LoggerInterface $logger, Formatter $formatter = null) { $this->logger = $logger; @@ -45,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { - $this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)), ['request' => $request]); + $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]); return $next($request)->then(function (ResponseInterface $response) use ($request) { $this->logger->info( - sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)), + sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)), [ 'request' => $request, 'response' => $response, @@ -60,7 +46,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl }, function (Exception $exception) use ($request) { if ($exception instanceof Exception\HttpException) { $this->logger->error( - sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)), + sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)), [ 'request' => $request, 'response' => $exception->getResponse(), @@ -69,7 +55,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl ); } else { $this->logger->error( - sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)), + sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)), [ 'request' => $request, 'exception' => $exception,