Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit decb656

Browse files
committed
Make PsrLogAdapter internal and create functions to add/remove loggers
1 parent c4913f1 commit decb656

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

‎src/PsrLogAdapter.php‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@
2727
use function MongoDB\Driver\Monitoring\removeSubscriber;
2828
use function sprintf;
2929

30+
/**
31+
* Integrates libmongoc/PHPC logging with one or more PSR-3 loggers.
32+
*
33+
* This class is internal and should not be utilized by applications. Logging
34+
* should be configured via the add_logger() and remove_logger() functions.
35+
*
36+
* @internal
37+
*/
3038
final class PsrLogAdapter implements LogSubscriber
3139
{
32-
/** @internal */
3340
public const LEVEL_EMERGENCY = 0;
3441
public const LEVEL_ALERT = 1;
3542
public const LEVEL_CRITICAL = 2;
@@ -78,20 +85,9 @@ public static function addLogger(LoggerInterface $logger): void
7885
addSubscriber($instance);
7986
}
8087

81-
public static function removeLogger(LoggerInterface $logger): void
82-
{
83-
$instance = self::getInstance();
84-
$instance->loggers->detach($logger);
85-
86-
if ($instance->loggers->count() === 0) {
87-
removeSubscriber($instance);
88-
}
89-
}
90-
9188
/**
9289
* Forwards a log message from libmongoc/PHPC to all registered PSR loggers.
9390
*
94-
* @internal
9591
* @see LogSubscriber::log()
9692
*/
9793
public function log(int $mongocLevel, string $domain, string $message): void
@@ -116,10 +112,20 @@ public function log(int $mongocLevel, string $domain, string $message): void
116112
}
117113
}
118114

115+
public static function removeLogger(LoggerInterface $logger): void
116+
{
117+
$instance = self::getInstance();
118+
$instance->loggers->detach($logger);
119+
120+
if ($instance->loggers->count() === 0) {
121+
removeSubscriber($instance);
122+
}
123+
}
124+
119125
/**
120126
* Writes a log message to all registered PSR loggers.
121127
*
122-
* @internal
128+
* This function is intended for internal use within the library.
123129
*/
124130
public static function writeLog(string $specLevel, string $domain, string $message): void
125131
{

‎src/functions.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use MongoDB\Exception\RuntimeException;
3232
use MongoDB\Operation\ListCollections;
3333
use MongoDB\Operation\WithTransaction;
34+
use Psr\Log\LoggerInterface;
3435
use ReflectionClass;
3536
use ReflectionException;
3637

@@ -46,6 +47,26 @@
4647
use function MongoDB\BSON\toPHP;
4748
use function substr;
4849

50+
/**
51+
* Registers a PSR-3 logger to receive log messages from the driver/library.
52+
*
53+
* Adding a registered logger is a NOP.
54+
*/
55+
function add_logger(LoggerInterface $logger): void
56+
{
57+
PsrLogAdapter::addLogger($logger);
58+
}
59+
60+
/**
61+
* Unregisters a PSR-3 logger.
62+
*
63+
* Removing an unregistered logger is a NOP.
64+
*/
65+
function remove_logger(LoggerInterface $logger): void
66+
{
67+
PsrLogAdapter::removeLogger($logger);
68+
}
69+
4970
/**
5071
* Check whether all servers support executing a write stage on a secondary.
5172
*

‎tests/PsrLogAdapterTest.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Psr\Log\LogLevel;
1212

1313
use function func_get_args;
14+
use function MongoDB\add_logger;
1415
use function MongoDB\Driver\Monitoring\mongoc_log;
16+
use function MongoDB\remove_logger;
1517
use function sprintf;
1618

1719
class PsrLogAdapterTest extends BaseTestCase
@@ -30,6 +32,31 @@ public function tearDown(): void
3032
PsrLogAdapter::removeLogger($this->logger);
3133
}
3234

35+
public function testAddAndRemoveLoggerFunctions(): void
36+
{
37+
$logger = $this->createTestPsrLogger();
38+
39+
mongoc_log(LogSubscriber::LEVEL_INFO, 'domain1', 'info1');
40+
PsrLogAdapter::writeLog(PsrLogAdapter::LEVEL_INFO, 'domain2', 'info2');
41+
42+
add_logger($logger);
43+
44+
mongoc_log(LogSubscriber::LEVEL_INFO, 'domain3', 'info3');
45+
PsrLogAdapter::writeLog(PsrLogAdapter::LEVEL_INFO, 'domain4', 'info4');
46+
47+
remove_logger($logger);
48+
49+
mongoc_log(LogSubscriber::LEVEL_INFO, 'domain5', 'info5');
50+
PsrLogAdapter::writeLog(PsrLogAdapter::LEVEL_INFO, 'domain6', 'info6');
51+
52+
$expectedLogs = [
53+
[LogLevel::INFO, 'info3', ['domain' => 'domain3']],
54+
[LogLevel::INFO, 'info4', ['domain' => 'domain4']],
55+
];
56+
57+
$this->assertSame($expectedLogs, $logger->logs);
58+
}
59+
3360
public function testLog(): void
3461
{
3562
/* This uses PHPC's internal mongoc_log() function to write messages

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /