Skip to content

Commit 3314d76

Browse files
author
Илья
committed
Added logger as YDB config
1 parent 528dbd6 commit 3314d76

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Ydb.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ public function __construct($config = [], LoggerInterface $logger = null)
107107
$this->database = $config['database'] ?? null;
108108
$this->iam_config = $config['iam_config'] ?? [];
109109

110-
if ($logger){
110+
if (!is_null($logger) && isset($config['logger'])){
111+
throw new \Exception('Logger set in 2 places');
112+
} else if (isset($config['logger']) && $config['logger'] instanceof LoggerInterface) {
113+
$this->logger = $config['logger'];
114+
} else if ($logger) {
111115
$this->logger = $logger;
112116
} else {
113117
$this->logger = new NullLogger();
@@ -351,4 +355,12 @@ 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+
}
354366
}

tests/LoggerCheckTest.php

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

0 commit comments

Comments
 (0)