Skip to content

Commit 0864c9d

Browse files
authored
Merge pull request #15 from bilfeldt/feature/log-with
Implement a new logWith-method + a LogAllFilter class and a NullLogger
2 parents 66e497a + 3bd3e16 commit 0864c9d

8 files changed

+75
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ The default logger and filter are specified in the package configuration `logger
8181
Http::log($context, $config, $logger, $filter)->get('https://example.com');
8282
// or
8383
Http::logWhen($condition, $context, $config, $logger, $filter)->get('https://example.com');
84+
// or the shorthand syntax which will use the LogAllFilter
85+
Http::logWith($logger)->get('https://example.com');
8486
```
8587
Note that the logger must implement `HttpLoggerInterface` while the filter must implement `HttpLoggingFilterInterface`.
8688

src/LaravelHttpClientLoggerServiceProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public function packageBooted()
5858
return $this;
5959
}
6060
});
61+
62+
PendingRequest::macro('logWith', function (HttpLoggerInterface $logger = null) {
63+
/** @var \Illuminate\Http\Client\PendingRequest $this */
64+
return $this->withMiddleware((new LoggingMiddleware($logger, new LogAllFilter()))->__invoke());
65+
});
6166
}
6267

6368
/**

src/LogAllFilter.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bilfeldt\LaravelHttpClientLogger;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
class LogAllFilter implements HttpLoggingFilterInterface
9+
{
10+
public function shouldLog(
11+
RequestInterface $request,
12+
ResponseInterface $response,
13+
float $sec,
14+
array $context = [],
15+
array $config = []
16+
): bool {
17+
return true;
18+
}
19+
}

src/MessageAccessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function filterRequest(RequestInterface $request): RequestInterface
138138
{
139139
/** @var RequestInterface $filtered */
140140
$filtered = $this->filterMessage($request);
141+
141142
return $filtered->withUri($this->getUri($request));
142143
}
143144

src/NullLogger.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bilfeldt\LaravelHttpClientLogger;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
class NullLogger implements HttpLoggerInterface
9+
{
10+
public function log(
11+
RequestInterface $request,
12+
ResponseInterface $response,
13+
float $sec,
14+
array $context = [],
15+
array $config = []
16+
): void {
17+
// Intentionally doing exactly nothing
18+
}
19+
}

tests/LogAllFilterTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bilfeldt\LaravelHttpClientLogger\Tests;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
class LogAllFilterTest extends TestCase
9+
{
10+
public function test_log_all_filter_returns_true()
11+
{
12+
$logAllFilter = new \Bilfeldt\LaravelHttpClientLogger\LogAllFilter();
13+
14+
$this->assertTrue($logAllFilter->shouldLog(
15+
$this->mock(RequestInterface::class),
16+
$this->mock(ResponseInterface::class),
17+
123
18+
));
19+
}
20+
}

tests/MacroTest.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ public function test_log()
99
// TODO: Implement
1010
}
1111

12-
public function test_log_with_context()
12+
public function test_log_parameter_context()
1313
{
1414
// TODO: Implement
1515
}
1616

17-
public function test_log_with_config()
17+
public function test_log_parameter_config()
1818
{
1919
// TODO: Implement
2020
}
2121

22-
public function test_log_with_logger()
22+
public function test_log_parameter_logger()
2323
{
2424
// TODO: Implement
2525
}
@@ -63,4 +63,9 @@ public function test_log_when_with_logger()
6363
{
6464
// TODO: Implement
6565
}
66+
67+
public function test_log_with()
68+
{
69+
// TODO: Implement
70+
}
6671
}

tests/MessageAccessorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function test_get_content()
147147

148148
public function test_filter_message()
149149
{
150-
$request = $this->messageAccessor->filter($this->request);
150+
$request = $this->messageAccessor->filterMessage($this->request);
151151

152152
// Note that it is required to use double quotes for the Carriage Return (\r) to work and have it on one line to pass on Windows
153153
$output = "POST /some-path/secret/should-not-be-removed?test=true&search=foo&filter%5Bfield1%5D=A&filter%5Bfield2%5D=B HTTP/1.1\r\nHost: ********.example.com:9000\r\nAccept: application/json\r\nContent-Type: application/json\r\nAuthorization: ********\r\n\r\n{\"data\":{\"foo\":\"bar\",\"baz\":[{\"field_1\":\"value1\",\"field_2\":\"value2\",\"password\":\"********\",\"secret\":\"this is not for everyone\"}]}}";

0 commit comments

Comments
 (0)