refactor(logging)

This commit is contained in:
Anatoliy
2026-06-09 16:11:15 +03:00
parent 7ea5836f44
commit d93b1569f5
9 changed files with 408 additions and 110 deletions

View File

@@ -15,6 +15,7 @@ use Apoloz\AlfaBank\Http\Middleware\VerifyJwsResponseMiddleware;
use Apoloz\AlfaBank\Http\Pagination\AlfaOffsetPaginator;
use Apoloz\AlfaBank\Traits\HasAlfaErrorHandling;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\Connector;
use Saloon\Http\OAuth2\GetClientCredentialsTokenRequest;
@@ -37,10 +38,13 @@ class AlfaBankConnector extends Connector implements HasPagination
public function __construct(
protected AlfaConfig $alfaConfig,
protected ?SignerInterface $signer = null
protected ?SignerInterface $signer = null,
protected bool $loggingEnabled = true,
protected ?LoggerInterface $logger = null,
) {
$this->tries = 3;
$this->retryInterval = 500;
$this->logger = $this->logger ?? new NullLogger;
}
public function resolveBaseUrl(): string
@@ -50,7 +54,12 @@ class AlfaBankConnector extends Connector implements HasPagination
public function boot(PendingRequest $pendingRequest): void
{
//
$pendingRequest->middleware()->onRequest(new RequestIdMiddleware);
if ($this->loggingEnabled) {
$pendingRequest->middleware()->onRequest(new LoggingRequestMiddleware($this->logger));
$pendingRequest->middleware()->onResponse(new LoggingResponseMiddleware($this->logger));
}
}
public function paginate(Request $request): OffsetPaginator
@@ -101,12 +110,19 @@ class AlfaBankConnector extends Connector implements HasPagination
}
// </SECTION>
// <SECTION name="logging_mutator" intent="Allow attaching a PSR-3 logger to the connector for debugging.">
// <BELIEF_STATE "Logging should be an optional, opt-in feature. This method provides a fluent interface to attach a logging middleware, which will automatically sanitize sensitive data before output.">
// <SECTION name="logging_mutator" intent="Attach a PSR-3 logger to the connector for debugging.">
// <BELIEF_STATE "Logging is enabled by default with a NullLogger (silent). Pass a real logger to start seeing output, or disable entirely via withoutLogging().">
public function withLogging(LoggerInterface $logger): static
{
$this->middleware()->onRequest(new LoggingRequestMiddleware($logger));
$this->middleware()->onResponse(new LoggingResponseMiddleware($logger));
$this->logger = $logger;
$this->loggingEnabled = true;
return $this;
}
public function withoutLogging(): static
{
$this->loggingEnabled = false;
return $this;
}
@@ -159,12 +175,5 @@ class AlfaBankConnector extends Connector implements HasPagination
// OAuth токен запросы требуют application/x-www-form-urlencoded
];
}
protected function defaultMiddleware(): array
{
return [
new RequestIdMiddleware,
];
}
// </SECTION>
}