Skip to content

Commit 3d41624

Browse files
authored
Merge pull request #86 from ydb-platform/add-sample-logger
Add simple logger
2 parents c1684b7 + f0e2128 commit 3d41624

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
* added simple std looger
2+
13
## 1.7.0
4+
25
* added environment credentials
36

47
## 1.6.0

src/Logger/SimpleStdLogger.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Logger;
4+
5+
class SimpleStdLogger implements \Psr\Log\LoggerInterface
6+
{
7+
const DEBUG = 7;
8+
const INFO = 6;
9+
const NOTICE = 5;
10+
const WARNING = 4;
11+
const ERROR = 3;
12+
const CRITICAL = 2;
13+
const ALERT = 1;
14+
const EMERGENCY = 0;
15+
16+
protected static $levels = [
17+
self::DEBUG => 'DEBUG',
18+
self::INFO => 'INFO',
19+
self::NOTICE => 'NOTICE',
20+
self::WARNING => 'WARNING',
21+
self::ERROR => 'ERROR',
22+
self::CRITICAL => 'CRITICAL',
23+
self::ALERT => 'ALERT',
24+
self::EMERGENCY => 'EMERGENCY',
25+
];
26+
27+
protected $level;
28+
public function __construct(int $level)
29+
{
30+
$this->level = $level;
31+
}
32+
33+
public function emergency($message, array $context = []): void
34+
{
35+
$this->log(self::EMERGENCY, $message, $context);
36+
}
37+
38+
public function alert($message, array $context = []): void
39+
{
40+
$this->log(self::EMERGENCY, $message, $context);
41+
}
42+
43+
public function critical($message, array $context = []): void
44+
{
45+
$this->log(self::EMERGENCY, $message, $context);
46+
}
47+
48+
public function error($message, array $context = []): void
49+
{
50+
$this->log(self::EMERGENCY, $message, $context);
51+
}
52+
53+
public function warning($message, array $context = []): void
54+
{
55+
$this->log(self::EMERGENCY, $message, $context);
56+
}
57+
58+
public function notice($message, array $context = []): void
59+
{
60+
$this->log(self::EMERGENCY, $message, $context);
61+
}
62+
63+
public function info($message, array $context = []): void
64+
{
65+
$this->log(self::EMERGENCY, $message, $context);
66+
}
67+
68+
public function debug($message, array $context = []): void
69+
{
70+
$this->log(self::EMERGENCY, $message, $context);
71+
}
72+
73+
public function log($level, $message, array $context = []): void
74+
{
75+
if ($level>$this->level) return;
76+
fwrite(STDERR,
77+
date("d/m/y H:i")." ".$level. " ".$message." ".json_encode($context)
78+
);
79+
}
80+
}

src/Ydb.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use YdbPlatform\Ydb\Exceptions\Ydb\SessionExpiredException;
1212
use YdbPlatform\Ydb\Retry\Retry;
1313
use YdbPlatform\Ydb\Retry\RetryParams;
14+
use YdbPlatform\Ydb\Logger\NullLogger;
1415

1516
require "Version.php";
1617

@@ -91,7 +92,11 @@ public function __construct($config = [], LoggerInterface $logger = null)
9192
$this->database = $config['database'] ?? null;
9293
$this->iam_config = $config['iam_config'] ?? [];
9394

94-
$this->logger = $logger;
95+
if ($logger){
96+
$this->logger = $logger;
97+
} else {
98+
$this->logger = new NullLogger();
99+
}
95100

96101
if(isset($config['credentials'])){
97102
$this->iam_config['credentials'] = $config['credentials'];

0 commit comments

Comments
 (0)