Skip to content

Commit 431a58b

Browse files
committed
wip Adapter to pipe ext-mongodb logs to a PSR-3 logger
1 parent 21781c9 commit 431a58b

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/Client.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
use MongoDB\Operation\ListDatabaseNames;
3838
use MongoDB\Operation\ListDatabases;
3939
use MongoDB\Operation\Watch;
40+
use Psr\Log\LoggerAwareInterface;
41+
use Psr\Log\LoggerInterface;
4042
use Throwable;
4143

4244
use function is_array;
4345
use function is_string;
4446

45-
class Client
47+
class Client implements LoggerAwareInterface
4648
{
4749
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
4850

@@ -329,6 +331,19 @@ public function selectDatabase(string $databaseName, array $options = [])
329331
return new Database($this->manager, $databaseName, $options);
330332
}
331333

334+
public function setLogger(LoggerInterface $logger): void
335+
{
336+
/* TODO: lazily initialize a PsrLogAdapter for this client and register
337+
* it with PHPC. Consider what happens if multiple clients instances in
338+
* an app register the same PSR logger. That logger will likely receive
339+
* duplicate messages, since PHPC's own checks would only prevent the
340+
* same PsrLogAdapter instance from being registered multiple times.
341+
*
342+
* Perhaps we can work around this by making PsrLogAdapter a singleton
343+
* or utilize a static map of PSR loggers.
344+
*/
345+
}
346+
332347
/**
333348
* Start a new client session.
334349
*

src/PsrLogAdapter.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/*
3+
* Copyright 2023-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB;
19+
20+
use MongoDB\Driver\Logging\Logger as DriverLoggerInterface;
21+
use Psr\Log\LoggerInterface as PsrLoggerInterface;
22+
use Psr\Log\LogLevel as PsrLogLevel;
23+
24+
class PsrLogAdapter implements DriverLoggerInterface
25+
{
26+
private PsrLoggerInterface $psrLogger;
27+
28+
public function __construct(PsrLoggerInterface $psrLogger)
29+
{
30+
$this->psrLogger = $psrLogger;
31+
}
32+
33+
public function log(int $level, string $domain, string $message): void
34+
{
35+
$this->psrLogger->log(
36+
$this->driverLevelToPsrLevel($level),
37+
$domain . ': ' . $message,
38+
);
39+
}
40+
41+
// TODO: Refactor this method as a private const array map on PsrLogAdapter
42+
private function driverLevelToPsrLevel(int $level): string
43+
{
44+
switch ($level) {
45+
/* libmongoc considers "critical" less severe than "error" so route
46+
* both levels to "error" in the PSR logger. */
47+
case DriverLoggerInterface::LEVEL_ERROR:
48+
case DriverLoggerInterface::LEVEL_CRITICAL:
49+
return PsrLogLevel::ERROR;
50+
51+
case DriverLoggerInterface::LEVEL_WARNING:
52+
return PsrLogLevel::WARNING;
53+
54+
case DriverLoggerInterface::LEVEL_MESSAGE:
55+
return PsrLogLevel::NOTICE;
56+
57+
case DriverLoggerInterface::LEVEL_INFO:
58+
return PsrLogLevel::INFO;
59+
60+
/* PSR does not define a "trace" level, so route both levels to
61+
* "debug". That said, trace logging is very verbose and should not
62+
* be encouraged when using a PSR logger. */
63+
case DriverLoggerInterface::LEVEL_DEBUG:
64+
case DriverLoggerInterface::LEVEL_TRACE:
65+
return PsrLogLevel::DEBUG;
66+
67+
/* A default case is intentionally not handled since libmongoc and
68+
* PHPC should avoid emitting bogus levels; however, we can consider
69+
* throwing an UnexpectedValueException here. */
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)