Skip to content

Implement a new logWith-method + a LogAllFilter class and a NullLogger #15

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 2 commits into from
Dec 18, 2021
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ The default logger and filter are specified in the package configuration `logger
Http::log($context, $config, $logger, $filter)->get('https://example.com');
// or
Http::logWhen($condition, $context, $config, $logger, $filter)->get('https://example.com');
// or the shorthand syntax which will use the LogAllFilter
Http::logWith($logger)->get('https://example.com');
```
Note that the logger must implement `HttpLoggerInterface` while the filter must implement `HttpLoggingFilterInterface`.

Expand Down
5 changes: 5 additions & 0 deletions src/LaravelHttpClientLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function packageBooted()
return $this;
}
});

PendingRequest::macro('logWith', function (HttpLoggerInterface $logger = null) {
/** @var \Illuminate\Http\Client\PendingRequest $this */
return $this->withMiddleware((new LoggingMiddleware($logger, new LogAllFilter()))->__invoke());
});
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/LogAllFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class LogAllFilter implements HttpLoggingFilterInterface
{
public function shouldLog(
RequestInterface $request,
ResponseInterface $response,
float $sec,
array $context = [],
array $config = []
): bool {
return true;
}
}
1 change: 1 addition & 0 deletions src/MessageAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function filterRequest(RequestInterface $request): RequestInterface
{
/** @var RequestInterface $filtered */
$filtered = $this->filterMessage($request);

return $filtered->withUri($this->getUri($request));
}

Expand Down
19 changes: 19 additions & 0 deletions src/NullLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class NullLogger implements HttpLoggerInterface
{
public function log(
RequestInterface $request,
ResponseInterface $response,
float $sec,
array $context = [],
array $config = []
): void {
// Intentionally doing exactly nothing
}
}
20 changes: 20 additions & 0 deletions tests/LogAllFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger\Tests;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class LogAllFilterTest extends TestCase
{
public function test_log_all_filter_returns_true()
{
$logAllFilter = new \Bilfeldt\LaravelHttpClientLogger\LogAllFilter();

$this->assertTrue($logAllFilter->shouldLog(
$this->mock(RequestInterface::class),
$this->mock(ResponseInterface::class),
123
));
}
}
11 changes: 8 additions & 3 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public function test_log()
// TODO: Implement
}

public function test_log_with_context()
public function test_log_parameter_context()
{
// TODO: Implement
}

public function test_log_with_config()
public function test_log_parameter_config()
{
// TODO: Implement
}

public function test_log_with_logger()
public function test_log_parameter_logger()
{
// TODO: Implement
}
Expand Down Expand Up @@ -63,4 +63,9 @@ public function test_log_when_with_logger()
{
// TODO: Implement
}

public function test_log_with()
{
// TODO: Implement
}
}
2 changes: 1 addition & 1 deletion tests/MessageAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function test_get_content()

public function test_filter_message()
{
$request = $this->messageAccessor->filter($this->request);
$request = $this->messageAccessor->filterMessage($this->request);

// 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
$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\"}]}}";
Expand Down