-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from simPod/loggers
Add support for sync query logging
- Loading branch information
Showing
8 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimPod\ClickHouseClient\Logger; | ||
|
||
final class LoggerChain implements SqlLogger | ||
{ | ||
/** @var SqlLogger[] */ | ||
private array $loggers; | ||
|
||
/** @param SqlLogger[] $loggers */ | ||
public function __construct(array $loggers = []) | ||
{ | ||
$this->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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimPod\ClickHouseClient\Logger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
final class PsrLogger implements SqlLogger | ||
{ | ||
private LoggerInterface $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function startQuery(string $sql, array $params = []) : void | ||
{ | ||
$this->logger->debug($sql, $params); | ||
} | ||
|
||
public function stopQuery() : void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimPod\ClickHouseClient\Logger; | ||
|
||
interface SqlLogger | ||
{ | ||
/** @param array<string, mixed> $params */ | ||
public function startQuery(string $sql, array $params = []) : void; | ||
|
||
public function stopQuery() : void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimPod\ClickHouseClient\Tests\Logger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SimPod\ClickHouseClient\Logger\LoggerChain; | ||
use SimPod\ClickHouseClient\Logger\SqlLogger; | ||
|
||
final class LoggerChainTest extends TestCase | ||
{ | ||
public function testLog() : void | ||
{ | ||
$logger = new class implements SqlLogger { | ||
public ?string $sql = null; | ||
|
||
/** @var array<string, mixed>|null $params */ | ||
public ?array $params = null; | ||
|
||
public bool $started = false; | ||
|
||
public bool $stopped = false; | ||
|
||
/** @inheritDoc */ | ||
public function startQuery(string $sql, array $params = []) : void | ||
{ | ||
$this->sql = $sql; | ||
$this->params = $params; | ||
$this->started = true; | ||
} | ||
|
||
public function stopQuery() : void | ||
{ | ||
$this->stopped = true; | ||
} | ||
}; | ||
|
||
$chain = new LoggerChain([$logger]); | ||
|
||
$chain->startQuery('sql', []); | ||
$chain->stopQuery(); | ||
|
||
self::assertSame('sql', $logger->sql); | ||
self::assertSame([], $logger->params); | ||
self::assertTrue($logger->started); | ||
self::assertTrue($logger->stopped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters