Skip to content

Better log display #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions spec/LoggerPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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
Expand Down
24 changes: 5 additions & 19 deletions src/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <joel.wurtz@gmail.com>
*/
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;
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand Down