Skip to content

Commit c0ab65c

Browse files
authored
MDEE-858: Make commerce-data-export Monolog implementation compatible with all supported AC versions (#444)
* MDEE-858: Make commerce-data-export Monolog implementation compatible with all supported AC versions
1 parent 6884b19 commit c0ab65c

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

DataExporter/Model/Logging/Monolog.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@
2424
/**
2525
* Proxy class to allow to instantiate custom Logger Interface
2626
*/
27-
class Monolog extends \Magento\Framework\Logger\Monolog implements CommerceDataExportLoggerInterface {
28-
27+
class Monolog extends \Magento\Framework\Logger\Monolog implements CommerceDataExportLoggerInterface
28+
{
29+
/**
30+
* @var LogRegistry
31+
*/
2932
private LogRegistry $logRegistry;
3033

34+
/**
35+
* @param LogRegistry $logRegistry
36+
* @param string $name
37+
* @param array $handlers
38+
* @param array $processors
39+
* @param DateTimeZone|null $timezone
40+
*/
3141
public function __construct(
3242
LogRegistry $logRegistry,
33-
string $name, array $handlers = [],
43+
string $name,
44+
array $handlers = [],
3445
array $processors = [],
35-
?DateTimeZone $timezone = null) {
46+
?DateTimeZone $timezone = null
47+
) {
3648
parent::__construct($name, $handlers, $processors, $timezone);
3749
$this->logRegistry = $logRegistry;
3850
}
@@ -74,9 +86,28 @@ public function logProgress($processedIdsNumber = null, $syncedItems = null): vo
7486
/**
7587
* @inheritDoc
7688
*/
77-
public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool
78-
{
79-
return parent::addRecord($level, $this->logRegistry->prepareMessage($message, []), $context, $datetime);
89+
public function addRecord(
90+
$level,
91+
string $message,
92+
array $context = [],
93+
DateTimeImmutable $datetime = null
94+
): bool {
95+
// Check if Monolog\Level class exists (for Monolog 3.x+ compatibility)
96+
if (class_exists('\Monolog\Level')) {
97+
// If it exists, use it as type hint
98+
if (!($level instanceof \Monolog\Level) && !\is_int($level)) {
99+
throw new \InvalidArgumentException('$level must be an instance of Monolog\Level or an integer');
100+
}
101+
// If it doesn't exist, only accept integers
102+
} elseif (!\is_int($level)) {
103+
throw new \InvalidArgumentException('$level must be an integer');
104+
}
105+
return parent::addRecord(
106+
$level,
107+
$this->logRegistry->prepareMessage($message, []),
108+
$context,
109+
$datetime
110+
);
80111
}
81112

82113
/**
@@ -94,4 +125,4 @@ public function complete(): void
94125
{
95126
$this->info('Complete');
96127
}
97-
}
128+
}

0 commit comments

Comments
 (0)