Skip to content

Commit 30d8a68

Browse files
author
Илья
committed
Add logger docs
1 parent 3314d76 commit 30d8a68

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

README.md

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

547547
You can chain these methods for convenience.
548+
549+
## Logging
550+
551+
For logging purposes, you need use class, which implements `\Psr\Log\LoggerInterface`.
552+
Example of using:
553+
554+
```php
555+
class SimpleLogger extends \Psr\Log\LoggerInterface
556+
{
557+
use LoggerTrait;
558+
public function log($level, string $message, array $context = []): void
559+
{
560+
fwrite(STDERR,
561+
date("d/m/y H:i:s")." ".self::getLevelName($level). " ".$message." ".json_encode($context)."\n"
562+
);
563+
}
564+
}
565+
566+
$config = [
567+
'logger' => new SimpleLogger()
568+
]
569+
$ydb = new \YdbPlatform\Ydb\Ydb($config);
570+
```
571+

src/Ydb.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public function __construct($config = [], LoggerInterface $logger = null)
109109

110110
if (!is_null($logger) && isset($config['logger'])){
111111
throw new \Exception('Logger set in 2 places');
112-
} else if (isset($config['logger']) && $config['logger'] instanceof LoggerInterface) {
113-
$this->logger = $config['logger'];
112+
} else if (isset($config['logger'])) {
113+
$this->setLogger($config['logger']);
114114
} else if ($logger) {
115-
$this->logger = $logger;
115+
$this->setLogger($logger);
116116
} else {
117-
$this->logger = new NullLogger();
117+
$this->setLogger(new NullLogger());
118118
}
119119

120120
$this->retry = new Retry($this->logger);
@@ -363,4 +363,9 @@ public function getLogger()
363363
{
364364
return $this->logger;
365365
}
366+
367+
protected function setLogger(LoggerInterface $logger){
368+
$this->logger = $logger;
369+
}
370+
366371
}

0 commit comments

Comments
 (0)