|
12 | 12 | use Magento\MagentoCloud\Filesystem\FileSystemException;
|
13 | 13 | use Monolog\Formatter\JsonFormatter;
|
14 | 14 |
|
15 |
| -if (\Monolog\Logger::API == 3) { |
| 15 | +/** |
| 16 | + * |
| 17 | + * Formatter for log messages for cloud.error.log |
| 18 | + */ |
| 19 | +class JsonErrorFormatter extends JsonFormatter |
| 20 | +{ |
16 | 21 | /**
|
17 |
| - * |
18 |
| - * Formatter for log messages for cloud.error.log |
| 22 | + * @var ErrorInfo |
19 | 23 | */
|
20 |
| - class JsonErrorFormatter extends JsonFormatter |
21 |
| - { |
22 |
| - /** |
23 |
| - * @var ErrorInfo |
24 |
| - */ |
25 |
| - private $errorInfo; |
| 24 | + private $errorInfo; |
26 | 25 |
|
27 |
| - /** |
28 |
| - * @var ReaderInterface |
29 |
| - */ |
30 |
| - private $reader; |
31 |
| - |
32 |
| - /** |
33 |
| - * @param ErrorInfo $errorInfo |
34 |
| - * @param ReaderInterface $reader |
35 |
| - * @param 1|2 $batchMode |
36 |
| - * @param bool $appendNewline |
37 |
| - */ |
38 |
| - public function __construct( |
39 |
| - ErrorInfo $errorInfo, |
40 |
| - ReaderInterface $reader, |
41 |
| - $batchMode = self::BATCH_MODE_JSON, |
42 |
| - $appendNewline = true |
43 |
| - ) { |
44 |
| - parent::__construct($batchMode, $appendNewline); |
| 26 | + /** |
| 27 | + * @var ReaderInterface |
| 28 | + */ |
| 29 | + private $reader; |
45 | 30 |
|
46 |
| - $this->errorInfo = $errorInfo; |
47 |
| - $this->reader = $reader; |
48 |
| - } |
| 31 | + /** |
| 32 | + * @param ErrorInfo $errorInfo |
| 33 | + * @param ReaderInterface $reader |
| 34 | + * @param 1|2 $batchMode |
| 35 | + * @param bool $appendNewline |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + ErrorInfo $errorInfo, |
| 39 | + ReaderInterface $reader, |
| 40 | + $batchMode = self::BATCH_MODE_JSON, |
| 41 | + $appendNewline = true |
| 42 | + ) { |
| 43 | + parent::__construct($batchMode, $appendNewline); |
| 44 | + |
| 45 | + $this->errorInfo = $errorInfo; |
| 46 | + $this->reader = $reader; |
| 47 | + } |
49 | 48 |
|
50 |
| - /** |
51 |
| - * Format record, skip logging if ErrorCode isn't passed. |
52 |
| - * |
53 |
| - * {@inheritDoc} |
54 |
| - */ |
55 |
| - public function format(\Monolog\LogRecord $record): string |
56 |
| - { |
| 49 | + /** |
| 50 | + * Format record, skip logging if ErrorCode isn't passed. |
| 51 | + * |
| 52 | + * {@inheritDoc} |
| 53 | + */ |
| 54 | + public function format(\Monolog\LogRecord|array $record): string |
| 55 | + { |
| 56 | + // Older Monolog versions. |
| 57 | + if (is_array($record)) { |
57 | 58 | try {
|
58 |
| - if (!isset($record->context['errorCode'])) { |
| 59 | + if (!isset($record['context']['errorCode'])) { |
59 | 60 | return '';
|
60 | 61 | }
|
61 |
| - |
| 62 | + |
62 | 63 | $loggedErrors = $this->reader->read();
|
63 |
| - |
64 |
| - if (isset($loggedErrors[$record->context['errorCode']])) { |
| 64 | + |
| 65 | + if (isset($loggedErrors[$record['context']['errorCode']])) { |
65 | 66 | return '';
|
66 | 67 | }
|
67 |
| - return $this->toJson($this->formatLog($record)) . PHP_EOL; |
| 68 | + |
| 69 | + return parent::format($this->formatLog($record)); |
68 | 70 | } catch (\Exception $exception) {
|
69 | 71 | return '';
|
70 | 72 | }
|
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Returns error info data based on errorCode. |
75 |
| - * |
76 |
| - * @param array $record |
77 |
| - * @return array |
78 |
| - * @throws FileSystemException |
79 |
| - */ |
80 |
| - private function formatLog(\Monolog\LogRecord $record): array |
81 |
| - { |
82 |
| - $errorCode = $record->context['errorCode']; |
83 |
| - $errorInfo = $this->errorInfo->get($errorCode); |
84 |
| - |
85 |
| - if (empty($errorInfo)) { |
86 |
| - $errorInfo = [ |
87 |
| - 'errorCode' => $errorCode, |
88 |
| - 'title' => $record->message ?? '' |
89 |
| - ]; |
90 |
| - } else { |
91 |
| - $errorInfo['errorCode'] = $errorCode; |
92 |
| - if (!empty($record->message)) { |
93 |
| - $errorInfo['title'] = $record->message; |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - if (!empty($record->context['suggestion'])) { |
98 |
| - $errorInfo['suggestion'] = $record->context['suggestion']; |
99 |
| - } |
100 |
| - |
101 |
| - ksort($errorInfo); |
102 |
| - |
103 |
| - return $errorInfo; |
104 |
| - } |
105 |
| - } |
106 |
| -} else { |
107 |
| - /** |
108 |
| - * |
109 |
| - * Formatter for log messages for cloud.error.log |
110 |
| - */ |
111 |
| - class JsonErrorFormatter extends JsonFormatter |
112 |
| - { |
113 |
| - /** |
114 |
| - * @var ErrorInfo |
115 |
| - */ |
116 |
| - private $errorInfo; |
117 |
| - |
118 |
| - /** |
119 |
| - * @var ReaderInterface |
120 |
| - */ |
121 |
| - private $reader; |
122 |
| - |
123 |
| - /** |
124 |
| - * @param ErrorInfo $errorInfo |
125 |
| - * @param ReaderInterface $reader |
126 |
| - * @param 1|2 $batchMode |
127 |
| - * @param bool $appendNewline |
128 |
| - */ |
129 |
| - public function __construct( |
130 |
| - ErrorInfo $errorInfo, |
131 |
| - ReaderInterface $reader, |
132 |
| - $batchMode = self::BATCH_MODE_JSON, |
133 |
| - $appendNewline = true |
134 |
| - ) { |
135 |
| - parent::__construct($batchMode, $appendNewline); |
136 |
| - |
137 |
| - $this->errorInfo = $errorInfo; |
138 |
| - $this->reader = $reader; |
139 |
| - } |
140 |
| - |
141 |
| - /** |
142 |
| - * Format record, skip logging if ErrorCode isn't passed. |
143 |
| - * |
144 |
| - * {@inheritDoc} |
145 |
| - */ |
146 |
| - public function format(array $record): string |
147 |
| - { |
| 73 | + } else if ($record instanceof \Monolog\LogRecord) { // Monolog version 3 or higher. |
148 | 74 | try {
|
149 |
| - if (!isset($record['context']['errorCode'])) { |
| 75 | + if (!isset($record->context['errorCode'])) { |
150 | 76 | return '';
|
151 | 77 | }
|
152 |
| - |
| 78 | + |
153 | 79 | $loggedErrors = $this->reader->read();
|
154 |
| - |
155 |
| - if (isset($loggedErrors[$record['context']['errorCode']])) { |
| 80 | + |
| 81 | + if (isset($loggedErrors[$record->context['errorCode']])) { |
156 | 82 | return '';
|
157 | 83 | }
|
158 | 84 |
|
159 |
| - return parent::format($this->formatLog($record)); |
| 85 | + return $this->toJson($this->formatLog($record->toArray())) . PHP_EOL; |
160 | 86 | } catch (\Exception $exception) {
|
161 | 87 | return '';
|
162 | 88 | }
|
163 | 89 | }
|
| 90 | + } |
164 | 91 |
|
165 |
| - /** |
166 |
| - * Returns error info data based on errorCode. |
167 |
| - * |
168 |
| - * @param array $record |
169 |
| - * @return array |
170 |
| - * @throws FileSystemException |
171 |
| - */ |
172 |
| - private function formatLog(array $record): array |
173 |
| - { |
174 |
| - $errorCode = $record['context']['errorCode']; |
175 |
| - $errorInfo = $this->errorInfo->get($errorCode); |
176 |
| - |
177 |
| - if (empty($errorInfo)) { |
178 |
| - $errorInfo = [ |
179 |
| - 'errorCode' => $errorCode, |
180 |
| - 'title' => $record['message'] ?? '' |
181 |
| - ]; |
182 |
| - } else { |
183 |
| - $errorInfo['errorCode'] = $errorCode; |
184 |
| - if (!empty($record['message'])) { |
185 |
| - $errorInfo['title'] = $record['message']; |
186 |
| - } |
| 92 | + /** |
| 93 | + * Returns error info data based on errorCode. |
| 94 | + * |
| 95 | + * @param array $record |
| 96 | + * @return array |
| 97 | + * @throws FileSystemException |
| 98 | + */ |
| 99 | + private function formatLog(array $record): array |
| 100 | + { |
| 101 | + $errorCode = $record['context']['errorCode']; |
| 102 | + $errorInfo = $this->errorInfo->get($errorCode); |
| 103 | + |
| 104 | + if (empty($errorInfo)) { |
| 105 | + $errorInfo = [ |
| 106 | + 'errorCode' => $errorCode, |
| 107 | + 'title' => $record['message'] ?? '' |
| 108 | + ]; |
| 109 | + } else { |
| 110 | + $errorInfo['errorCode'] = $errorCode; |
| 111 | + if (!empty($record['message'])) { |
| 112 | + $errorInfo['title'] = $record['message']; |
187 | 113 | }
|
| 114 | + } |
188 | 115 |
|
189 |
| - if (!empty($record['context']['suggestion'])) { |
190 |
| - $errorInfo['suggestion'] = $record['context']['suggestion']; |
191 |
| - } |
| 116 | + if (!empty($record['context']['suggestion'])) { |
| 117 | + $errorInfo['suggestion'] = $record['context']['suggestion']; |
| 118 | + } |
192 | 119 |
|
193 |
| - ksort($errorInfo); |
| 120 | + ksort($errorInfo); |
194 | 121 |
|
195 |
| - return $errorInfo; |
196 |
| - } |
| 122 | + return $errorInfo; |
197 | 123 | }
|
198 |
| - |
199 | 124 | }
|
0 commit comments