Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.4.1 - unreleased
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be a new minor. Ie 1.5.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to 1.5.0. Should I set a release date?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Na, this is fine. We will set a release date just before we tag


- Added request uri to log context

## 1.4.0 - 2024-10-31

- Support PHP 8.3 and 8.4
Expand Down
11 changes: 10 additions & 1 deletion src/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
{
$start = hrtime(true) / 1E6;
$uid = uniqid('', true);
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['uid' => $uid]);
$this->logger->info(
sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)),
[
'uid' => $uid,
'uri' => (string) $request->getUri(),
]
);

return $next($request)->then(function (ResponseInterface $response) use ($start, $uid, $request) {
$milliseconds = (int) round(hrtime(true) / 1E6 - $start);
Expand All @@ -41,6 +47,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
[
'milliseconds' => $milliseconds,
'uid' => $uid,
'uri' => (string) $request->getUri(),
]
);

Expand All @@ -55,6 +62,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
'exception' => $exception,
'milliseconds' => $milliseconds,
'uid' => $uid,
'uri' => (string) $request->getUri(),
]
);
} else {
Expand All @@ -64,6 +72,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
'exception' => $exception,
'milliseconds' => $milliseconds,
'uid' => $uid,
'uri' => (string) $request->getUri(),
]
);
}
Expand Down
12 changes: 10 additions & 2 deletions tests/LoggerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ public function testLogsRequestAndResponse()
$response = new Response();

$actualResponse = $this->plugin->handleRequest(
new Request('GET', 'http://example.com/'),
new Request('GET', 'http://example.com/path?query=value#fragment'),
fn (RequestInterface $req) => new FulfilledPromise($response),
function () {}
)->wait();

self::assertSame($response, $actualResponse);

self::assertCount(2, $this->logger->logMessages);
self::assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
self::assertSame("Sending request:\nGET http://example.com/path?query=value#fragment 1.1", $this->logger->logMessages[0]['info']);
self::assertSame('http://example.com/path?query=value#fragment', $this->logger->logMessages[0]['context']['uri']);
self::assertSame("Received response:\n200 OK 1.1", $this->logger->logMessages[1]['info']);
self::assertSame('http://example.com/path?query=value#fragment', $this->logger->logMessages[1]['context']['uri']);
}

public function testLogsRequestException()
Expand All @@ -55,7 +57,9 @@ function () {}
} catch (NetworkException $exception) {
self::assertCount(2, $this->logger->logMessages);
self::assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
self::assertSame('http://example.com/', $this->logger->logMessages[0]['context']['uri']);
self::assertSame("Error:\nNetwork error\nwhen sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[1]['error']);
self::assertSame('http://example.com/', $this->logger->logMessages[1]['context']['uri']);

throw $exception;
}
Expand All @@ -75,8 +79,12 @@ function () {}
// Expected
$this->assertCount(2, $this->logger->logMessages);
$this->assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
self::assertSame('http://example.com/', $this->logger->logMessages[0]['context']['uri']);

// Ensure there's an error log for the exception
$this->assertStringContainsString("Error:\nNot Found", $this->logger->logMessages[1]['error']);
self::assertSame('http://example.com/', $this->logger->logMessages[1]['context']['uri']);

throw $exception;
}
}
Expand Down