1 | <?php |
||
2 | namespace App; |
||
3 | |||
4 | use App\Monolog\FormatHelper; |
||
5 | use App\Monolog\LoggerFactory; |
||
6 | use Bitrix\Main\Diag\Debug; |
||
0 ignored issues
–
show
|
|||
7 | use Monolog\Logger; |
||
8 | use Psr\Log\LoggerInterface; |
||
9 | use Psr\Log\LogLevel; |
||
10 | |||
11 | class Log implements LoggerInterface { |
||
12 | |||
13 | private $channel; |
||
14 | |||
15 | /** |
||
16 | * @param string $channel |
||
17 | */ |
||
18 | public function __construct($channel = '') { |
||
19 | |||
20 | if(!empty($channel)) { |
||
21 | $this->channel = $channel; |
||
22 | } else { |
||
23 | $this->channel = ($_ENV['APP_LOG_BITRIX_CHANNEL'] ?: 'bitrix'); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param mixed $message |
||
29 | * @param array $context |
||
30 | */ |
||
31 | public function alert($message, $context = []): void |
||
32 | { |
||
33 | try { |
||
34 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
35 | $message = FormatHelper::stringfyMessage($message); |
||
36 | $logger->alert($message, (array) $context); |
||
37 | } catch (\Exception $e) { |
||
38 | //there may be too many open files |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param mixed $message |
||
44 | * @param array $context |
||
45 | */ |
||
46 | public function critical($message, $context = []): void |
||
47 | { |
||
48 | try { |
||
49 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
50 | $message = FormatHelper::stringfyMessage($message); |
||
51 | $logger->critical($message, (array) $context); |
||
52 | } catch (\Exception $e) { |
||
53 | //there may be too many open files |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param mixed $message |
||
59 | * @param array $context |
||
60 | */ |
||
61 | public function error($message, $context = []): void |
||
62 | { |
||
63 | if ($this->isDebugEnabled(Logger::ERROR)) { |
||
64 | try { |
||
65 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
66 | $message = FormatHelper::stringfyMessage($message); |
||
67 | $logger->error($message, (array) $context); |
||
68 | } catch (\Exception $e) { |
||
69 | //there may be too many open files |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param mixed $message |
||
76 | * @param array $context |
||
77 | */ |
||
78 | public function warning($message, $context = []): void |
||
79 | { |
||
80 | if ($this->isDebugEnabled(Logger::WARNING)) { |
||
81 | try { |
||
82 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
83 | $message = FormatHelper::stringfyMessage($message); |
||
84 | $logger->warning($message, (array) $context); |
||
85 | } catch (\Exception $e) { |
||
86 | //there may be too many open files |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param mixed $message |
||
93 | * @param array $context |
||
94 | */ |
||
95 | public function notice($message, $context = []): void |
||
96 | { |
||
97 | if ($this->isDebugEnabled(Logger::NOTICE)) { |
||
98 | try { |
||
99 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
100 | $message = FormatHelper::stringfyMessage($message); |
||
101 | $logger->notice($message, (array) $context); |
||
102 | } catch (\Exception $e) { |
||
103 | //there may be too many open files |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param mixed $message |
||
110 | * @param array $context |
||
111 | */ |
||
112 | public function info($message, $context = []): void |
||
113 | { |
||
114 | if ($this->isDebugEnabled(Logger::INFO)) { |
||
115 | try { |
||
116 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
117 | $message = FormatHelper::stringfyMessage($message); |
||
118 | $logger->info($message, (array) $context); |
||
119 | } catch (\Exception $e) { |
||
120 | //there may be too many open files |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param mixed $message |
||
127 | * @param array $context |
||
128 | */ |
||
129 | public function debug($message, $context = []): void |
||
130 | { |
||
131 | if ($this->isDebugEnabled(Logger::DEBUG)) { |
||
132 | try { |
||
133 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
134 | $message = FormatHelper::stringfyMessage($message); |
||
135 | $logger->debug($message, (array) $context); |
||
136 | } catch (\Exception $e) { |
||
137 | //there may be too many open files |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param mixed $message |
||
144 | * @param array $context |
||
145 | */ |
||
146 | public function emergency($message, $context = []): void |
||
147 | { |
||
148 | try { |
||
149 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
150 | $message = FormatHelper::stringfyMessage($message); |
||
151 | $logger->emergency($message, (array) $context); |
||
152 | } catch (\Exception $e) { |
||
153 | //there may be too many open files |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param mixed $level |
||
159 | * @param mixed $message |
||
160 | * @param array $context |
||
161 | */ |
||
162 | public function log($level, $message, $context = []): void |
||
163 | { |
||
164 | if ($this->isDebugEnabled($level)) { |
||
165 | try { |
||
166 | $logger = LoggerFactory::getInstance($this->channel, $context); |
||
167 | $message = FormatHelper::stringfyMessage($message); |
||
168 | $logger->log($level, $message, (array) $context); |
||
169 | } catch (\Exception $e) { |
||
170 | //there may be too many open files |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param mixed $level |
||
177 | * @param mixed $message |
||
178 | * @param array $context |
||
179 | */ |
||
180 | public function telegram($level, $message, $context = []): void |
||
181 | { |
||
182 | if (!$this->isDebugEnabled($level)) { |
||
183 | return; |
||
184 | } |
||
185 | $this->log($level, $message, $context); |
||
186 | |||
187 | $logger = LoggerFactory::getInstance(LoggerFactory::TELEGRAM_CHANNEL, $context); |
||
188 | |||
189 | if($logger) { |
||
190 | try { |
||
191 | $message = FormatHelper::stringfyTelegramMessage($message, (array) $context); |
||
192 | $logger->log($level, $message, $context); |
||
193 | } catch (\Exception $e) { |
||
194 | $this->logInnerException($e); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param mixed $level |
||
201 | * @return bool |
||
202 | */ |
||
203 | private function isDebugEnabled($level) |
||
204 | { |
||
205 | if (defined('FORCE_DEBUG') && FORCE_DEBUG) { |
||
0 ignored issues
–
show
|
|||
206 | return true; |
||
207 | } |
||
208 | |||
209 | $level = Logger::toMonologLevel($level); |
||
210 | |||
211 | $minDebugLevel = ($_ENV['APP_DEBUG_LEVEL'] ?: LogLevel::DEBUG); |
||
212 | $minDebugLevel = Logger::toMonologLevel($minDebugLevel); |
||
213 | |||
214 | if($level >= $minDebugLevel) { |
||
215 | return true; |
||
216 | } |
||
217 | return false; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param \Exception $exception |
||
222 | */ |
||
223 | protected function logInnerException(\Exception $exception) |
||
224 | { |
||
225 | //there may be too many open files |
||
226 | try { |
||
227 | $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/'); |
||
228 | $logPath = rtrim($logPath, "/"); |
||
229 | file_put_contents($logPath . '/monolog_error.log', (string) $exception); |
||
230 | } catch(\Exception $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
231 | |||
232 | } |
||
233 | } |
||
234 | |||
235 | public static function cleanLogs($daysAgo = 15) { |
||
236 | |||
237 | $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/'); |
||
238 | $command = sprintf("find %s -mindepth 1 -type f -mtime +%d | xargs rm", $logPath, $daysAgo); |
||
239 | exec($command); |
||
240 | return sprintf('\App\Log::cleanLogs(%d);', $daysAgo); |
||
241 | } |
||
242 | } |
||
243 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths