From 9a17386c9e28fa546095560f424c408287d84409 Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Mon, 16 Sep 2024 11:20:43 +0200 Subject: [PATCH] Override Symfony's logger with GLPI's one --- dependency_injection/services.php | 8 +++ src/Glpi/Log/LegacyGlobalLogger.php | 96 +++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/Glpi/Log/LegacyGlobalLogger.php diff --git a/dependency_injection/services.php b/dependency_injection/services.php index 4d33234341c..97c80e12f63 100644 --- a/dependency_injection/services.php +++ b/dependency_injection/services.php @@ -37,6 +37,7 @@ use Glpi\DependencyInjection\PublicService; use Glpi\Http\Firewall; use Glpi\Http\FirewallInterface; +use Glpi\Log\LegacyGlobalLogger; return static function (ContainerConfigurator $container): void { $projectDir = dirname(__DIR__); @@ -65,6 +66,13 @@ ->lazy() ; + /** + * Override Symfony's logger. + * @see \Symfony\Component\HttpKernel\DependencyInjection\LoggerPass + */ + $services->set('logger', LegacyGlobalLogger::class); + + // Development-only if ($container->env() === 'development') { $container->extension('web_profiler', [ 'toolbar' => true, diff --git a/src/Glpi/Log/LegacyGlobalLogger.php b/src/Glpi/Log/LegacyGlobalLogger.php new file mode 100644 index 00000000000..9f44b596291 --- /dev/null +++ b/src/Glpi/Log/LegacyGlobalLogger.php @@ -0,0 +1,96 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace Glpi\Log; + +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; + +class LegacyGlobalLogger implements LoggerInterface +{ + private function getLogger(): LoggerInterface + { + /** + * @var null|\Psr\Log\LoggerInterface $PHPLOGGER + */ + global $PHPLOGGER; + + return $PHPLOGGER ?? new NullLogger(); + } + + public function emergency(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->emergency($message, $context); + } + + public function alert(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->alert($message, $context); + } + + public function critical(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->critical($message, $context); + } + + public function error(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->error($message, $context); + } + + public function warning(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->warning($message, $context); + } + + public function notice(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->notice($message, $context); + } + + public function info(\Stringable|string $message, array $context = []): void + { + $this->getLogger()->info($message, $context); + } + + public function debug(\Stringable|string $message, array $context = []): void + { + // Debug is disabled for now because Symfony generates a lot of debug logs that GLPI doesn't need yet. + } + + public function log($level, \Stringable|string $message, array $context = []): void + { + $this->getLogger()->log($level, $message, $context); + } +}