Skip to content

Commit 5bf3c2d

Browse files
committed
Introduce Logger plugin
1 parent 1cb8755 commit 5bf3c2d

File tree

10 files changed

+66
-45
lines changed

10 files changed

+66
-45
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Naming used here is the same as in ClickHouse docs.
1616
- All [ClickHouse Formats](https://clickhouse.yandex/docs/en/interfaces/formats/) support
1717
- Logging ([PSR-3 compliant](https://www.php-fig.org/psr/psr-3/))
1818
- SQL Factory for [parameters "binding"](#parameters-binding)
19-
- Dependency only on PSR interfaces, Guzzle A+ Promises for async requests and Safe
2019

2120
## Contents
2221

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"require": {
2424
"php-64bit": "^7.4",
2525
"guzzlehttp/promises": "^1.4",
26+
"php-http/client-common": "^2.0",
2627
"psr/http-client": "^1.0",
2728
"psr/http-factory": "^1.0",
2829
"psr/http-message": "^1.0",

src/Client/Http/LoggerPlugin.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimPod\ClickHouseClient\Client\Http;
6+
7+
use Http\Client\Common\Plugin;
8+
use Http\Promise\Promise;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use SimPod\ClickHouseClient\Logger\SqlLogger;
12+
use Throwable;
13+
14+
use function uniqid;
15+
16+
final class LoggerPlugin implements Plugin
17+
{
18+
private SqlLogger $logger;
19+
20+
public function __construct(SqlLogger $logger)
21+
{
22+
$this->logger = $logger;
23+
}
24+
25+
public function handleRequest(RequestInterface $request, callable $next, callable $first) : Promise
26+
{
27+
$id = uniqid('', true);
28+
$this->logger->startQuery($id, (string) $request->getBody());
29+
30+
return $next($request)->then(
31+
function (ResponseInterface $response) use ($id) : ResponseInterface {
32+
$this->logger->stopQuery($id);
33+
34+
return $response;
35+
},
36+
function (Throwable $throwable) use ($id) : void {
37+
$this->logger->stopQuery($id);
38+
39+
throw $throwable;
40+
}
41+
);
42+
}
43+
}

src/Client/PsrClickHouseAsyncClient.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use GuzzleHttp\Promise\PromiseInterface;
1010
use Http\Client\HttpAsyncClient;
1111
use Psr\Http\Message\ResponseInterface;
12-
use Psr\Log\LoggerInterface;
1312
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
1413
use SimPod\ClickHouseClient\Client\Http\RequestOptions;
1514
use SimPod\ClickHouseClient\Exception\ServerError;
@@ -24,8 +23,6 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient
2423

2524
private RequestFactory $requestFactory;
2625

27-
private LoggerInterface $logger;
28-
2926
private string $endpoint;
3027

3128
/** @var array<string, float|int|string> */
@@ -37,14 +34,12 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient
3734
public function __construct(
3835
HttpAsyncClient $asyncClient,
3936
RequestFactory $requestFactory,
40-
LoggerInterface $logger,
4137
string $endpoint,
4238
array $defaultParameters = [],
4339
?DateTimeZone $clickHouseTimeZone = null
4440
) {
4541
$this->asyncClient = $asyncClient;
4642
$this->requestFactory = $requestFactory;
47-
$this->logger = $logger;
4843
$this->endpoint = $endpoint;
4944
$this->defaultParameters = $defaultParameters;
5045
$this->sqlFactory = new SqlFactory(new ValueFormatter($clickHouseTimeZone));
@@ -90,8 +85,6 @@ private function executeRequest(
9085
array $requestParameters = [],
9186
?callable $processResponse = null
9287
) : PromiseInterface {
93-
$this->logger->debug($sql, $requestParameters);
94-
9588
$request = $this->requestFactory->prepareRequest(
9689
$this->endpoint,
9790
new RequestOptions(

src/Client/PsrClickHouseClient.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use SimPod\ClickHouseClient\Exception\CannotInsert;
1313
use SimPod\ClickHouseClient\Exception\ServerError;
1414
use SimPod\ClickHouseClient\Format\Format;
15-
use SimPod\ClickHouseClient\Logger\SqlLogger;
1615
use SimPod\ClickHouseClient\Output\Output;
1716
use SimPod\ClickHouseClient\Sql\Escaper;
1817
use SimPod\ClickHouseClient\Sql\SqlFactory;
@@ -31,8 +30,6 @@ class PsrClickHouseClient implements ClickHouseClient
3130

3231
private RequestFactory $requestFactory;
3332

34-
private SqlLogger $logger;
35-
3633
private string $endpoint;
3734

3835
/** @var array<string, float|int|string> */
@@ -46,14 +43,12 @@ class PsrClickHouseClient implements ClickHouseClient
4643
public function __construct(
4744
ClientInterface $client,
4845
RequestFactory $requestFactory,
49-
SqlLogger $logger,
5046
string $endpoint,
5147
array $defaultParameters = [],
5248
?DateTimeZone $clickHouseTimeZone = null
5349
) {
5450
$this->client = $client;
5551
$this->requestFactory = $requestFactory;
56-
$this->logger = $logger;
5752
$this->endpoint = $endpoint;
5853
$this->defaultParameters = $defaultParameters;
5954
$this->valueFormatter = new ValueFormatter($clickHouseTimeZone);
@@ -171,12 +166,8 @@ private function executeRequest(string $sql, array $requestParameters = []) : Re
171166
)
172167
);
173168

174-
$this->logger->startQuery($sql);
175-
176169
$response = $this->client->sendRequest($request);
177170

178-
$this->logger->stopQuery();
179-
180171
if ($response->getStatusCode() !== 200) {
181172
throw ServerError::fromResponse($response);
182173
}

src/Logger/LoggerChain.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace SimPod\ClickHouseClient\Logger;
66

7+
use function array_filter;
8+
79
final class LoggerChain implements SqlLogger
810
{
911
/** @var SqlLogger[] */
@@ -12,21 +14,23 @@ final class LoggerChain implements SqlLogger
1214
/** @param SqlLogger[] $loggers */
1315
public function __construct(array $loggers = [])
1416
{
15-
$this->loggers = $loggers;
17+
$this->loggers = array_filter(
18+
$loggers,
19+
static fn (SqlLogger $logger) : bool => ! $logger instanceof self
20+
);
1621
}
1722

18-
/** @inheritdoc */
19-
public function startQuery(string $sql, array $params = []) : void
23+
public function startQuery(string $id, string $sql) : void
2024
{
2125
foreach ($this->loggers as $logger) {
22-
$logger->startQuery($sql, $params);
26+
$logger->startQuery($id, $sql);
2327
}
2428
}
2529

26-
public function stopQuery() : void
30+
public function stopQuery(string $id) : void
2731
{
2832
foreach ($this->loggers as $logger) {
29-
$logger->stopQuery();
33+
$logger->stopQuery($id);
3034
}
3135
}
3236
}

src/Logger/PsrLogger.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ public function __construct(LoggerInterface $logger)
1515
$this->logger = $logger;
1616
}
1717

18-
/** @inheritdoc */
19-
public function startQuery(string $sql, array $params = []) : void
18+
public function startQuery(string $id, string $sql) : void
2019
{
21-
$this->logger->debug($sql, $params);
20+
$this->logger->debug($sql);
2221
}
2322

24-
public function stopQuery() : void
23+
public function stopQuery(string $id) : void
2524
{
2625
}
2726
}

src/Logger/SqlLogger.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
interface SqlLogger
88
{
9-
/** @param array<string, mixed> $params */
10-
public function startQuery(string $sql, array $params = []) : void;
9+
public function startQuery(string $id, string $sql) : void;
1110

12-
public function stopQuery() : void;
11+
public function stopQuery(string $id) : void;
1312
}

tests/Logger/LoggerChainTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,33 @@ final class LoggerChainTest extends TestCase
1313
public function testLog() : void
1414
{
1515
$logger = new class implements SqlLogger {
16-
public ?string $sql = null;
16+
public string $id;
1717

18-
/** @var array<string, mixed>|null $params */
19-
public ?array $params = null;
18+
public ?string $sql = null;
2019

2120
public bool $started = false;
2221

2322
public bool $stopped = false;
2423

25-
/** @inheritDoc */
26-
public function startQuery(string $sql, array $params = []) : void
24+
public function startQuery(string $id, string $sql) : void
2725
{
26+
$this->id = $id;
2827
$this->sql = $sql;
29-
$this->params = $params;
3028
$this->started = true;
3129
}
3230

33-
public function stopQuery() : void
31+
public function stopQuery(string $id) : void
3432
{
3533
$this->stopped = true;
3634
}
3735
};
3836

3937
$chain = new LoggerChain([$logger]);
4038

41-
$chain->startQuery('sql', []);
42-
$chain->stopQuery();
39+
$chain->startQuery('a', 'sql');
40+
$chain->stopQuery('a');
4341

4442
self::assertSame('sql', $logger->sql);
45-
self::assertSame([], $logger->params);
4643
self::assertTrue($logger->started);
4744
self::assertTrue($logger->stopped);
4845
}

tests/WithClient.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
use Http\Client\Curl\Client;
88
use Nyholm\Psr7\Factory\Psr17Factory;
9-
use Psr\Log\Test\TestLogger;
109
use SimPod\ClickHouseClient\Client\ClickHouseAsyncClient;
1110
use SimPod\ClickHouseClient\Client\ClickHouseClient;
1211
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
1312
use SimPod\ClickHouseClient\Client\PsrClickHouseAsyncClient;
1413
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
15-
use SimPod\ClickHouseClient\Logger\PsrLogger;
1614

1715
use function assert;
1816
use function getenv;
@@ -64,7 +62,6 @@ public function restartClickHouseClient() : void
6462
new Psr17Factory(),
6563
new Psr17Factory()
6664
),
67-
new PsrLogger(new TestLogger()),
6865
$endpoint,
6966
$defaultParameters
7067
);
@@ -78,7 +75,6 @@ public function restartClickHouseClient() : void
7875
new Psr17Factory(),
7976
new Psr17Factory()
8077
),
81-
new PsrLogger(new TestLogger()),
8278
$endpoint,
8379
$defaultParameters
8480
);
@@ -90,7 +86,6 @@ public function restartClickHouseClient() : void
9086
new Psr17Factory(),
9187
new Psr17Factory()
9288
),
93-
new TestLogger(),
9489
$endpoint,
9590
$defaultParameters
9691
);

0 commit comments

Comments
 (0)