Skip to content

Commit 8a4c3a8

Browse files
committed
Remove trace logging and introduce static method to write logs
1 parent 6d08297 commit 8a4c3a8

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

src/PsrLogAdapter.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
namespace MongoDB;
1919

2020
use MongoDB\Driver\Logging\Logger as DriverLogger;
21+
use MongoDB\Exception\UnexpectedValueException;
2122
use Psr\Log\LoggerInterface as PsrLogger;
2223
use Psr\Log\LogLevel as PsrLogLevel;
2324
use SplObjectStorage;
2425

2526
use function MongoDB\Driver\Logging\addLogger;
2627
use function MongoDB\Driver\Logging\removeLogger;
28+
use function sprintf;
2729

2830
final class PsrLogAdapter implements DriverLogger
2931
{
@@ -41,10 +43,6 @@ final class PsrLogAdapter implements DriverLogger
4143
DriverLogger::LEVEL_MESSAGE => PsrLogLevel::NOTICE,
4244
DriverLogger::LEVEL_INFO => PsrLogLevel::INFO,
4345
DriverLogger::LEVEL_DEBUG => PsrLogLevel::DEBUG,
44-
/* PSR does not define a "trace" level, so map it to "debug" in the PSR
45-
* logger. That said, trace logging is very verbose and should not be
46-
* encouraged when using a PSR logger. */
47-
DriverLogger::LEVEL_TRACE => PsrLogLevel::DEBUG,
4846
];
4947

5048
public static function addLogger(PsrLogger $psrLogger): void
@@ -56,21 +54,26 @@ public static function addLogger(PsrLogger $psrLogger): void
5654
addLogger($instance);
5755
}
5856

59-
public static function getInstance(): self
57+
/**
58+
* Pipes a log message from PHPC to all registered PSR loggers.
59+
*
60+
* @internal
61+
* @see DriverLogger::log()
62+
*/
63+
public function log(int $level, string $domain, string $message): void
6064
{
61-
if (self::$instance === null) {
62-
self::$instance = new self();
65+
if (! isset(self::PSR_LEVELS[$level])) {
66+
throw new UnexpectedValueException(sprintf(
67+
'Expected level to be >= %d and <= %d, %d given for domain "%s" and message: %s',
68+
DriverLogger::LEVEL_ERROR,
69+
DriverLogger::LEVEL_DEBUG,
70+
$level,
71+
$domain,
72+
$message,
73+
));
6374
}
6475

65-
return self::$instance;
66-
}
67-
68-
public function log(int $level, string $domain, string $message): void
69-
{
7076
$instance = self::getInstance();
71-
/* A default case is intentionally not handled since libmongoc and
72-
* PHPC should avoid emitting bogus levels; however, we can consider
73-
* throwing an UnexpectedValueException here. */
7477
$psrLevel = self::PSR_LEVELS[$level];
7578
$context = ['domain' => $domain];
7679

@@ -89,8 +92,27 @@ public static function removeLogger(PsrLogger $psrLogger): void
8992
}
9093
}
9194

95+
/**
96+
* Writes a log message to all registered PSR loggers.
97+
*
98+
* @internal
99+
*/
100+
public static function writeLog(int $level, string $domain, string $message): void
101+
{
102+
self::getInstance()->log($level, $domain, $message);
103+
}
104+
92105
private function __construct()
93106
{
94107
$this->psrLoggers = new SplObjectStorage();
95108
}
109+
110+
private static function getInstance(): self
111+
{
112+
if (self::$instance === null) {
113+
self::$instance = new self();
114+
}
115+
116+
return self::$instance;
117+
}
96118
}

tests/PsrLogAdapterTest.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace MongoDB\Tests;
44

55
use MongoDB\Driver\Logging\Logger as DriverLogger;
6+
use MongoDB\Exception\UnexpectedValueException;
67
use MongoDB\PsrLogAdapter;
78
use PHPUnit\Framework\TestCase as BaseTestCase;
89
use Psr\Log\AbstractLogger;
910
use Psr\Log\LoggerInterface;
1011
use Psr\Log\LogLevel;
1112

1213
use function func_get_args;
13-
use function MongoDB\Driver\Logging\log;
14+
use function sprintf;
1415

1516
class PsrLogAdapterTest extends BaseTestCase
1617
{
@@ -28,19 +29,32 @@ public function tearDown(): void
2829
PsrLogAdapter::removeLogger($this->logger);
2930
}
3031

31-
public function testDriverLogLevelsAreMappedToPsrLogLevels(): void
32+
/**
33+
* @testWith [-1]
34+
* [6]
35+
*/
36+
public function testWriteLogWithInvalidLevel(int $level): void
37+
{
38+
$this->expectException(UnexpectedValueException::class);
39+
$this->expectExceptionMessage(sprintf('Expected level to be >= 0 and <= 5, %d given for domain "domain" and message: message', $level));
40+
41+
PsrLogAdapter::writeLog($level, 'domain', 'message');
42+
}
43+
44+
public function testWriteLogMapsDriverLogLevelsToPsrLogLevels(): void
3245
{
3346
PsrLogAdapter::addLogger($this->logger);
3447

35-
log(DriverLogger::LEVEL_ERROR, 'error');
36-
log(DriverLogger::LEVEL_CRITICAL, 'critical');
37-
log(DriverLogger::LEVEL_WARNING, 'warning');
38-
log(DriverLogger::LEVEL_MESSAGE, 'message');
39-
log(DriverLogger::LEVEL_INFO, 'info');
40-
log(DriverLogger::LEVEL_DEBUG, 'debug');
41-
log(DriverLogger::LEVEL_TRACE, 'trace');
48+
$domain = 'domain';
49+
50+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_ERROR, $domain, 'error');
51+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_CRITICAL, $domain, 'critical');
52+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_WARNING, $domain, 'warning');
53+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_MESSAGE, $domain, 'message');
54+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_INFO, $domain, 'info');
55+
PsrLogAdapter::writeLog(DriverLogger::LEVEL_DEBUG, $domain, 'debug');
4256

43-
$context = ['domain' => 'php'];
57+
$context = ['domain' => $domain];
4458

4559
$expectedLogs = [
4660
[LogLevel::ERROR, 'error', $context],
@@ -49,7 +63,6 @@ public function testDriverLogLevelsAreMappedToPsrLogLevels(): void
4963
[LogLevel::NOTICE, 'message', $context],
5064
[LogLevel::INFO, 'info', $context],
5165
[LogLevel::DEBUG, 'debug', $context],
52-
[LogLevel::DEBUG, 'trace', $context],
5366
];
5467

5568
$this->assertSame($this->logger->logs, $expectedLogs);

0 commit comments

Comments
 (0)