24
24
/**
25
25
* Proxy class to allow to instantiate custom Logger Interface
26
26
*/
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
+ */
29
32
private LogRegistry $ logRegistry ;
30
33
34
+ /**
35
+ * @param LogRegistry $logRegistry
36
+ * @param string $name
37
+ * @param array $handlers
38
+ * @param array $processors
39
+ * @param DateTimeZone|null $timezone
40
+ */
31
41
public function __construct (
32
42
LogRegistry $ logRegistry ,
33
- string $ name , array $ handlers = [],
43
+ string $ name ,
44
+ array $ handlers = [],
34
45
array $ processors = [],
35
- ?DateTimeZone $ timezone = null ) {
46
+ ?DateTimeZone $ timezone = null
47
+ ) {
36
48
parent ::__construct ($ name , $ handlers , $ processors , $ timezone );
37
49
$ this ->logRegistry = $ logRegistry ;
38
50
}
@@ -74,9 +86,28 @@ public function logProgress($processedIdsNumber = null, $syncedItems = null): vo
74
86
/**
75
87
* @inheritDoc
76
88
*/
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
+ );
80
111
}
81
112
82
113
/**
@@ -94,4 +125,4 @@ public function complete(): void
94
125
{
95
126
$ this ->info ('Complete ' );
96
127
}
97
- }
128
+ }
0 commit comments