|
| 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\Monitoring\LogSubscriber; |
| 21 | +use MongoDB\Exception\UnexpectedValueException; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | +use Psr\Log\LogLevel; |
| 24 | +use SplObjectStorage; |
| 25 | + |
| 26 | +use function MongoDB\Driver\Monitoring\addSubscriber; |
| 27 | +use function MongoDB\Driver\Monitoring\removeSubscriber; |
| 28 | +use function sprintf; |
| 29 | + |
| 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 | + */ |
| 38 | +final class PsrLogAdapter implements LogSubscriber |
| 39 | +{ |
| 40 | + public const EMERGENCY = 0; |
| 41 | + public const ALERT = 1; |
| 42 | + public const CRITICAL = 2; |
| 43 | + public const ERROR = 3; |
| 44 | + public const WARN = 4; |
| 45 | + public const NOTICE = 5; |
| 46 | + public const INFO = 6; |
| 47 | + public const DEBUG = 7; |
| 48 | + public const TRACE = 8; |
| 49 | + |
| 50 | + private static ?self $instance = null; |
| 51 | + |
| 52 | + /** @psalm-var SplObjectStorage<LoggerInterface, null> */ |
| 53 | + private SplObjectStorage $loggers; |
| 54 | + |
| 55 | + private const SPEC_TO_PSR = [ |
| 56 | + self::EMERGENCY => LogLevel::EMERGENCY, |
| 57 | + self::ALERT => LogLevel::ALERT, |
| 58 | + self::CRITICAL => LogLevel::CRITICAL, |
| 59 | + self::ERROR => LogLevel::ERROR, |
| 60 | + self::WARN => LogLevel::WARNING, |
| 61 | + self::NOTICE => LogLevel::NOTICE, |
| 62 | + self::INFO => LogLevel::INFO, |
| 63 | + self::DEBUG => LogLevel::DEBUG, |
| 64 | + // PSR does not define a "trace" level, so map it to "debug" |
| 65 | + self::TRACE => LogLevel::DEBUG, |
| 66 | + ]; |
| 67 | + |
| 68 | + private const MONGOC_TO_PSR = [ |
| 69 | + LogSubscriber::LEVEL_ERROR => LogLevel::ERROR, |
| 70 | + /* libmongoc considers "critical" less severe than "error" so map it to |
| 71 | + * "error" in the PSR logger. */ |
| 72 | + LogSubscriber::LEVEL_CRITICAL => LogLevel::ERROR, |
| 73 | + LogSubscriber::LEVEL_WARNING => LogLevel::WARNING, |
| 74 | + LogSubscriber::LEVEL_MESSAGE => LogLevel::NOTICE, |
| 75 | + LogSubscriber::LEVEL_INFO => LogLevel::INFO, |
| 76 | + LogSubscriber::LEVEL_DEBUG => LogLevel::DEBUG, |
| 77 | + ]; |
| 78 | + |
| 79 | + public static function addLogger(LoggerInterface $logger): void |
| 80 | + { |
| 81 | + $instance = self::getInstance(); |
| 82 | + |
| 83 | + $instance->loggers->attach($logger); |
| 84 | + |
| 85 | + addSubscriber($instance); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Forwards a log message from libmongoc/PHPC to all registered PSR loggers. |
| 90 | + * |
| 91 | + * @see LogSubscriber::log() |
| 92 | + */ |
| 93 | + public function log(int $mongocLevel, string $domain, string $message): void |
| 94 | + { |
| 95 | + if (! isset(self::MONGOC_TO_PSR[$mongocLevel])) { |
| 96 | + throw new UnexpectedValueException(sprintf( |
| 97 | + 'Expected level to be >= %d and <= %d, %d given for domain "%s" and message: %s', |
| 98 | + LogSubscriber::LEVEL_ERROR, |
| 99 | + LogSubscriber::LEVEL_DEBUG, |
| 100 | + $mongocLevel, |
| 101 | + $domain, |
| 102 | + $message, |
| 103 | + )); |
| 104 | + } |
| 105 | + |
| 106 | + $instance = self::getInstance(); |
| 107 | + $psrLevel = self::MONGOC_TO_PSR[$mongocLevel]; |
| 108 | + $context = ['domain' => $domain]; |
| 109 | + |
| 110 | + foreach ($instance->loggers as $logger) { |
| 111 | + $logger->log($psrLevel, $message, $context); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 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 | + |
| 125 | + /** |
| 126 | + * Writes a log message to all registered PSR loggers. |
| 127 | + * |
| 128 | + * This function is intended for internal use within the library. |
| 129 | + */ |
| 130 | + public static function writeLog(int $specLevel, string $domain, string $message): void |
| 131 | + { |
| 132 | + if (! isset(self::SPEC_TO_PSR[$specLevel])) { |
| 133 | + throw new UnexpectedValueException(sprintf( |
| 134 | + 'Expected level to be >= %d and <= %d, %d given for domain "%s" and message: %s', |
| 135 | + self::EMERGENCY, |
| 136 | + self::TRACE, |
| 137 | + $specLevel, |
| 138 | + $domain, |
| 139 | + $message, |
| 140 | + )); |
| 141 | + } |
| 142 | + |
| 143 | + $instance = self::getInstance(); |
| 144 | + $psrLevel = self::SPEC_TO_PSR[$specLevel]; |
| 145 | + $context = ['domain' => $domain]; |
| 146 | + |
| 147 | + foreach ($instance->loggers as $logger) { |
| 148 | + $logger->log($psrLevel, $message, $context); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private function __construct() |
| 153 | + { |
| 154 | + $this->loggers = new SplObjectStorage(); |
| 155 | + } |
| 156 | + |
| 157 | + private static function getInstance(): self |
| 158 | + { |
| 159 | + return self::$instance ??= new self(); |
| 160 | + } |
| 161 | +} |
0 commit comments