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
25 changes: 24 additions & 1 deletion app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ public static function isInstalled($options = [])
* log facility (??)
*
* @param array|object|string $message
* @param null|int|Level::* $level
* @param null|int|Level::*|string $level
* @param null|string $file
* @param bool $forceLog
*/
Expand Down Expand Up @@ -906,8 +906,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
{
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())) {
throw new InvalidArgumentException('Level "' . $level . '" is not defined, use one of: ' . implode(', ', $reflectionClass->getConstants()));
}

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