Skip to content

Commit

Permalink
Override Symfony's logger with GLPI's one
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval authored and cedric-anne committed Sep 16, 2024
1 parent cbe52b2 commit 9a17386
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dependency_injection/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down Expand Up @@ -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,
Expand Down
96 changes: 96 additions & 0 deletions src/Glpi/Log/LegacyGlobalLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

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);
}
}

0 comments on commit 9a17386

Please sign in to comment.