diff --git a/src/Client/PsrClickHouseAsyncClient.php b/src/Client/PsrClickHouseAsyncClient.php index 3302890..49e2ac9 100644 --- a/src/Client/PsrClickHouseAsyncClient.php +++ b/src/Client/PsrClickHouseAsyncClient.php @@ -105,7 +105,6 @@ private function executeRequest( $promise = promise_for($this->asyncClient->sendAsyncRequest($request)); return $promise->then( - /** @return mixed */ static function (ResponseInterface $response) use ($processResponse) { if ($response->getStatusCode() !== 200) { throw ServerError::fromResponse($response); diff --git a/src/Client/PsrClickHouseClient.php b/src/Client/PsrClickHouseClient.php index 8520aa8..56a787d 100644 --- a/src/Client/PsrClickHouseClient.php +++ b/src/Client/PsrClickHouseClient.php @@ -7,12 +7,12 @@ use DateTimeZone; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\ResponseInterface; -use Psr\Log\LoggerInterface; use SimPod\ClickHouseClient\Client\Http\RequestFactory; use SimPod\ClickHouseClient\Client\Http\RequestOptions; use SimPod\ClickHouseClient\Exception\CannotInsert; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\Format; +use SimPod\ClickHouseClient\Logger\SqlLogger; use SimPod\ClickHouseClient\Output\Output; use SimPod\ClickHouseClient\Sql\Escaper; use SimPod\ClickHouseClient\Sql\SqlFactory; @@ -31,7 +31,7 @@ class PsrClickHouseClient implements ClickHouseClient private RequestFactory $requestFactory; - private LoggerInterface $logger; + private SqlLogger $logger; private string $endpoint; @@ -46,7 +46,7 @@ class PsrClickHouseClient implements ClickHouseClient public function __construct( ClientInterface $client, RequestFactory $requestFactory, - LoggerInterface $logger, + SqlLogger $logger, string $endpoint, array $defaultParameters = [], ?DateTimeZone $clickHouseTimeZone = null @@ -162,8 +162,6 @@ public function insertWithFormat(string $table, Format $inputFormat, string $dat /** @param array $requestParameters */ private function executeRequest(string $sql, array $requestParameters = []) : ResponseInterface { - $this->logger->debug($sql, $requestParameters); - $request = $this->requestFactory->prepareRequest( $this->endpoint, new RequestOptions( @@ -173,7 +171,12 @@ private function executeRequest(string $sql, array $requestParameters = []) : Re ) ); + $this->logger->startQuery($sql); + $response = $this->client->sendRequest($request); + + $this->logger->stopQuery(); + if ($response->getStatusCode() !== 200) { throw ServerError::fromResponse($response); } diff --git a/src/Logger/LoggerChain.php b/src/Logger/LoggerChain.php new file mode 100644 index 0000000..111fef1 --- /dev/null +++ b/src/Logger/LoggerChain.php @@ -0,0 +1,32 @@ +loggers = $loggers; + } + + /** @inheritdoc */ + public function startQuery(string $sql, array $params = []) : void + { + foreach ($this->loggers as $logger) { + $logger->startQuery($sql, $params); + } + } + + public function stopQuery() : void + { + foreach ($this->loggers as $logger) { + $logger->stopQuery(); + } + } +} diff --git a/src/Logger/PsrLogger.php b/src/Logger/PsrLogger.php new file mode 100644 index 0000000..4cfc545 --- /dev/null +++ b/src/Logger/PsrLogger.php @@ -0,0 +1,27 @@ +logger = $logger; + } + + /** @inheritdoc */ + public function startQuery(string $sql, array $params = []) : void + { + $this->logger->debug($sql, $params); + } + + public function stopQuery() : void + { + } +} diff --git a/src/Logger/SqlLogger.php b/src/Logger/SqlLogger.php new file mode 100644 index 0000000..a670beb --- /dev/null +++ b/src/Logger/SqlLogger.php @@ -0,0 +1,13 @@ + $params */ + public function startQuery(string $sql, array $params = []) : void; + + public function stopQuery() : void; +} diff --git a/tests/WithClient.php b/tests/WithClient.php index 094ea06..152019b 100644 --- a/tests/WithClient.php +++ b/tests/WithClient.php @@ -6,13 +6,13 @@ use Http\Client\Curl\Client; use Nyholm\Psr7\Factory\Psr17Factory; -use Psr\Log\NullLogger; use Psr\Log\Test\TestLogger; use SimPod\ClickHouseClient\Client\ClickHouseAsyncClient; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Client\Http\RequestFactory; use SimPod\ClickHouseClient\Client\PsrClickHouseAsyncClient; use SimPod\ClickHouseClient\Client\PsrClickHouseClient; +use SimPod\ClickHouseClient\Logger\PsrLogger; use function assert; use function getenv; @@ -64,7 +64,7 @@ public function restartClickHouseClient() : void new Psr17Factory(), new Psr17Factory() ), - new NullLogger(), + new PsrLogger(new TestLogger()), $endpoint, $defaultParameters ); @@ -78,7 +78,7 @@ public function restartClickHouseClient() : void new Psr17Factory(), new Psr17Factory() ), - new TestLogger(), + new PsrLogger(new TestLogger()), $endpoint, $defaultParameters );