Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,10 @@ public static function isInstalled($options = [])
/**
* log facility (??)
*
* @param array|object|string $message
* @param null|int|Level::* $level
* @param null|string $file
* @param bool $forceLog
* @param array|object|string $message
* @param null|int|Level::*|string $level
* @param null|string $file
* @param bool $forceLog
*/
public static function log($message, $level = null, $file = '', $forceLog = false, array $context = [])
{
Expand Down Expand Up @@ -921,8 +921,31 @@ public static function log($message, $level = null, $file = '', $forceLog = fals
$levelValue = $level->value;
} elseif (is_null($level)) {
$levelValue = Level::Debug->value;
} elseif (is_string($level) && !is_numeric($level)) {
// PSR 3 Log level
try {
$levelValue = Level::fromName($level)->value;
} catch (ValueError) {
$levelValue = Level::Debug->value; // fallback to debug level
}
} else {
$levelValue = (int) $level;
// change RFC 5424 Log Level into Monolog.
if ($levelValue >= 0 && $levelValue <= 7) {
$levelValue = (match ($levelValue) {
7 => Level::Debug,
6 => Level::Info,
5 => Level::Notice,
4 => Level::Warning,
3 => Level::Error,
2 => Level::Critical,
1 => Level::Alert,
0 => Level::Emergency,
})->value;
} elseif ($levelValue < 100) {
// unknown levels are treated as debug
$levelValue = Level::Debug->value; // fallback to debug level
}
}

if (!self::$_isDeveloperMode && $levelValue > $maxLogLevel && !$forceLog) {
Expand Down
41 changes: 41 additions & 0 deletions app/code/core/Mage/Core/Helper/PsrLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* @copyright For copyright and license information, read the COPYING.txt file.
* @link /COPYING.txt
* @license Open Software License (OSL 3.0)
* @package Mage_Core
*/

use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;

/**
* Provides a PSR-3 LoggerInterface implementation that wraps Mage::log().
*
* This class enables integration with PSR-3 compatible logging libraries and tools
* by forwarding log messages to the native Mage::log() method. It allows Magento
* modules and external libraries to use standardized logging practices within the
* OpenMage framework.
*
* @package Mage_Core
*/
class Mage_Core_Helper_PsrLogger extends Mage_Core_Helper_Abstract implements LoggerInterface

Check warning on line 27 in app/code/core/Mage/Core/Helper/PsrLogger.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename class "Mage_Core_Helper_PsrLogger" to match the regular expression ^[A-Z][a-zA-Z0-9]*$.

See more on https://sonarcloud.io/project/issues?id=OpenMage_magento-lts&issues=AZzX2Rbv1_JK1a_wN2H4&open=AZzX2Rbv1_JK1a_wN2H4&pullRequest=5144
{
use LoggerTrait;

public function log($level, string|\Stringable $message, array $context = []): void
{
// unknown log level need to throw an InvalidArgumentException
$reflectionClass = new ReflectionClass(LogLevel::class);
if (!in_array($level, $reflectionClass->getConstants())) {

Check warning on line 35 in app/code/core/Mage/Core/Helper/PsrLogger.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure that this accessibility bypass is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenMage_magento-lts&issues=AZzX2Rbv1_JK1a_wN2H5&open=AZzX2Rbv1_JK1a_wN2H5&pullRequest=5144
throw new InvalidArgumentException('Level "' . $level . '" is not defined, use one of: ' . implode(', ', $reflectionClass->getConstants()));

Check warning on line 36 in app/code/core/Mage/Core/Helper/PsrLogger.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure that this accessibility bypass is safe here.

See more on https://sonarcloud.io/project/issues?id=OpenMage_magento-lts&issues=AZzX2Rbv1_JK1a_wN2H6&open=AZzX2Rbv1_JK1a_wN2H6&pullRequest=5144
}

Mage::log((string) $message, $level, null, false, $context);
}
}
Loading