Skip to content

Commit 04b50d9

Browse files
authored
Added logger as YDB config
Added logger as YDB config
2 parents f4dd1aa + a4d8a26 commit 04b50d9

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* add logger as Ydb config
12
* added snapshot mode in noninteractive transaction
23

34
## 1.12.0

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,19 @@ Methods of the query builder:
546546
- `txControl(\Ydb\Table\TransactionControl $tx_control)` - transaction control with custom settings
547547

548548
You can chain these methods for convenience.
549+
550+
## Logging
551+
552+
For logging purposes, you need use class, which implements `\Psr\Log\LoggerInterface`.
553+
YDB-PHP-SDK has build-in loggers in `YdbPlatform\Ydb\Logger` namespace:
554+
* `NullLogger` - default
555+
* `SimpleStdLogger($level)` - logger, which push logs in STDERR.
556+
557+
Example of using:
558+
```php
559+
$config = [
560+
'logger' => new \YdbPlatform\Ydb\Logger\SimpleStdLogger(\YdbPlatform\Ydb\Logger\SimpleStdLogger::INFO)
561+
]
562+
$ydb = new \YdbPlatform\Ydb\Ydb($config);
563+
```
564+

src/Ydb.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ public function __construct($config = [], LoggerInterface $logger = null)
107107
$this->database = $config['database'] ?? null;
108108
$this->iam_config = $config['iam_config'] ?? [];
109109

110-
if ($logger){
111-
$this->logger = $logger;
110+
if (!is_null($logger) && isset($config['logger'])){
111+
throw new \Exception('Logger set in 2 places');
112+
} else if (isset($config['logger'])) {
113+
$this->setLogger($config['logger']);
114+
} else if ($logger) {
115+
$this->setLogger($logger);
112116
} else {
113-
$this->logger = new NullLogger();
117+
$this->setLogger(new NullLogger());
114118
}
115119

116120
$this->retry = new Retry($this->logger);
@@ -351,4 +355,21 @@ public function discoveryInterval()
351355
{
352356
return $this->discoveryInterval;
353357
}
358+
359+
/**
360+
* @return LoggerInterface|NullLogger
361+
*/
362+
public function getLogger()
363+
{
364+
return $this->logger;
365+
}
366+
367+
/**
368+
* @param LoggerInterface $logger
369+
* @return void
370+
*/
371+
protected function setLogger(LoggerInterface $logger){
372+
$this->logger = $logger;
373+
}
374+
354375
}

tests/LoggerCheckTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Logger\NullLogger;
8+
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
9+
use YdbPlatform\Ydb\Ydb;
10+
11+
class LoggerCheckTest extends TestCase{
12+
13+
public function testExceptExceptionInConfigs(){
14+
$config = [
15+
'logger' => new SimpleStdLogger(7)
16+
];
17+
$this->expectException('Exception');
18+
new Ydb($config, new NullLogger());
19+
}
20+
public function testCheckUseLoggerFromConfig(){
21+
$logger = new SimpleStdLogger(7);
22+
$config = [
23+
'logger' => $logger
24+
];
25+
$ydb = new Ydb($config);
26+
$this->assertEquals($logger, $ydb->getLogger());
27+
}
28+
public function testCheckUseLoggerFromParam(){
29+
$logger = new SimpleStdLogger(7);
30+
$config = [];
31+
$ydb = new Ydb($config, $logger);
32+
$this->assertEquals($logger, $ydb->getLogger());
33+
}
34+
public function testCheckUseNullLogger(){
35+
$config = [];
36+
$ydb = new Ydb($config);
37+
$this->assertInstanceOf(NullLogger::class, $ydb->getLogger());
38+
}
39+
40+
public function testThrowExceptionOnNonLoggerObject(){
41+
$config = [
42+
'logger' => new AnonymousAuthentication()
43+
];
44+
$this->expectException('TypeError');
45+
$ydb = new Ydb($config);
46+
$this->assertInstanceOf(NullLogger::class, $ydb->getLogger());
47+
}
48+
}

0 commit comments

Comments
 (0)