Skip to content

Commit 6715e4a

Browse files
authored
Merge pull request #2 from lyrixx/better-logs
Better log display
2 parents d6c2ac7 + 6611920 commit 6715e4a

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

spec/LoggerPluginSpec.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ function it_logs_request_and_response(
3939
$formatter->formatRequest($request)->willReturn('GET / 1.1');
4040
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
4141

42-
$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
43-
$logger->info('Receive response: "200 OK 1.1" for request: "GET / 1.1"', ['request' => $request, 'response' => $response])->shouldBeCalled();
42+
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
43+
$logger->info("Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1", ['request' => $request, 'response' => $response])->shouldBeCalled();
4444

4545
$next = function () use ($response) {
4646
return new FulfilledPromise($response->getWrappedObject());
@@ -55,8 +55,8 @@ function it_logs_exception(LoggerInterface $logger, Formatter $formatter, Reques
5555

5656
$exception = new NetworkException('Cannot connect', $request->getWrappedObject());
5757

58-
$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
59-
$logger->error('Error: "Cannot connect" when emitting request: "GET / 1.1"', ['request' => $request, 'exception' => $exception])->shouldBeCalled();
58+
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
59+
$logger->error("Error:\nCannot connect\nwhen sending request:\nGET / 1.1", ['request' => $request, 'exception' => $exception])->shouldBeCalled();
6060

6161
$next = function () use ($exception) {
6262
return new RejectedPromise($exception);
@@ -76,8 +76,8 @@ function it_logs_response_within_exception(
7676

7777
$exception = new HttpException('Forbidden', $request->getWrappedObject(), $response->getWrappedObject());
7878

79-
$logger->info('Emit request: "GET / 1.1"', ['request' => $request])->shouldBeCalled();
80-
$logger->error('Error: "Forbidden" with response: "403 Forbidden 1.1" when emitting request: "GET / 1.1"', [
79+
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
80+
$logger->error("Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1", [
8181
'request' => $request,
8282
'response' => $response,
8383
'exception' => $exception

src/LoggerPlugin.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,15 @@
1111
use Psr\Log\LoggerInterface;
1212

1313
/**
14-
* Log request, response and exception for a HTTP Client.
14+
* Log request, response and exception for an HTTP Client.
1515
*
1616
* @author Joel Wurtz <joel.wurtz@gmail.com>
1717
*/
1818
final class LoggerPlugin implements Plugin
1919
{
20-
/**
21-
* Logger to log request / response / exception for a http call.
22-
*
23-
* @var LoggerInterface
24-
*/
2520
private $logger;
26-
27-
/**
28-
* Formats a request/response as string.
29-
*
30-
* @var Formatter
31-
*/
3221
private $formatter;
3322

34-
/**
35-
* @param LoggerInterface $logger
36-
*/
3723
public function __construct(LoggerInterface $logger, Formatter $formatter = null)
3824
{
3925
$this->logger = $logger;
@@ -45,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
4531
*/
4632
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4733
{
48-
$this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)), ['request' => $request]);
34+
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);
4935

5036
return $next($request)->then(function (ResponseInterface $response) use ($request) {
5137
$this->logger->info(
52-
sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
38+
sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
5339
[
5440
'request' => $request,
5541
'response' => $response,
@@ -60,7 +46,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6046
}, function (Exception $exception) use ($request) {
6147
if ($exception instanceof Exception\HttpException) {
6248
$this->logger->error(
63-
sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
49+
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)),
6450
[
6551
'request' => $request,
6652
'response' => $exception->getResponse(),
@@ -69,7 +55,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6955
);
7056
} else {
7157
$this->logger->error(
72-
sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)),
58+
sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)),
7359
[
7460
'request' => $request,
7561
'exception' => $exception,

0 commit comments

Comments
 (0)