1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
logformatter.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Diag;
11
12use Bitrix\Main\Type;
13use Bitrix\Main\DB;
14
16{
17 protected const DELIMITER = '----------';
18
19 protected $showArguments;
20 protected $argMaxChars;
21
22 public function __construct($showArguments = false, $argMaxChars = 30)
23 {
24 $this->showArguments = $showArguments;
25 $this->argMaxChars = $argMaxChars;
26 }
27
32 public function format($message, array $context = []): string
33 {
34 // Implementors MAY have special handling for the passed objects. If that is not the case, implementors MUST cast it to a string.
36
37 if (!isset($context['delimiter']))
38 {
39 $context['delimiter'] = static::DELIMITER;
40 }
41
42 if (!isset($context['date']))
43 {
44 $context['date'] = new Type\DateTime();
45 }
46
47 if (!isset($context['host']))
48 {
49 $context['host'] = $_SERVER['HTTP_HOST'] ?? '';
50 }
51
52 // Placeholder names MUST be delimited with a single opening brace { and a single closing brace }. There MUST NOT be any whitespace between the delimiters and the placeholder name.
53 $replace = [];
54 foreach ($context as $key => $val)
55 {
56 $replace['{' . $key . '}'] = $this->castToString($val, $key);
57 }
58
59 return strtr($message, $replace);
60 }
61
68 protected function castToString($value, $placeholder = null): string
69 {
70 if (!is_string($value))
71 {
72 if (is_object($value))
73 {
74 if ($placeholder == 'date' && $value instanceof Type\Date)
75 {
76 $value = $this->formatDate($value);
77 }
78 elseif ($placeholder == 'exception' && $value instanceof \Throwable)
79 {
80 $value = $this->formatException($value);
81 }
82 elseif (method_exists($value, '__toString'))
83 {
84 $value = (string)$value;
85 }
86 else
87 {
88 $value = $this->formatMixed($value);
89 }
90 }
91 else
92 {
93 if ($placeholder == 'trace' && is_array($value))
94 {
95 $value = $this->formatTrace($value);
96 }
97 else
98 {
99 $value = $this->formatMixed($value);
100 }
101 }
102 }
103 return $value;
104 }
105
111 protected function formatException(\Throwable $exception): string
112 {
113 $result = '[' . get_class($exception) . '] ';
114
115 if ($exception instanceof \ErrorException)
116 {
117 $result .= static::severityToString($exception->getSeverity());
118 }
119
120 $result .= "\n" . $exception->getMessage() . ' (' . $exception->getCode() . ')' . "\n";
121
122 if ($exception instanceof DB\SqlQueryException)
123 {
124 $result .= $exception->getQuery() . "\n";
125 }
126
127 $file = $exception->getFile();
128 if ($file)
129 {
130 $result .= $file . ':' . $exception->getLine() . "\n";
131 }
132
133 return $result;
134 }
135
141 protected function formatTrace(array $trace): string
142 {
143 $result = '';
144
145 foreach ($trace as $traceNum => $traceInfo)
146 {
147 $traceLine = '#'.$traceNum.': ';
148
149 if (isset($traceInfo['class']))
150 {
151 $traceLine .= $traceInfo['class'] . $traceInfo['type'];
152 }
153
154 if (isset($traceInfo['function']))
155 {
156 $traceLine .= $traceInfo['function'];
157 if(isset($traceInfo['args']))
158 {
159 $traceLine .= $this->formatArguments($traceInfo['args']);
160 }
161 }
162
163 $traceLine .= "\n\t";
164 if (isset($traceInfo['file']))
165 {
166 $traceLine .= $traceInfo['file'] . ':' . $traceInfo['line'];
167 }
168
169 $result .= $traceLine . "\n";
170 }
171
172 return $result;
173 }
174
180 public function formatArguments($args)
181 {
182 if ($args !== null)
183 {
184 $arguments = [];
185 foreach ($args as $arg)
186 {
187 $arguments[] = $this->formatArgument($arg);
188 }
189
190 return '(' . implode(', ', $arguments) . ')';
191 }
192 return '()';
193 }
194
200 public function formatArgument($arg)
201 {
202 if (!$this->showArguments)
203 {
204 return gettype($arg);
205 }
206
207 switch (gettype($arg))
208 {
209 case 'boolean':
210 $result = $arg ? 'true' : 'false';
211 break;
212 case 'NULL':
213 $result = 'null';
214 break;
215 case 'integer':
216 case 'double':
217 $result = (string)$arg;
218 break;
219 case 'string':
220 if (is_callable($arg, false, $callableName))
221 {
222 $result = 'fs:' . $callableName;
223 }
224 elseif (class_exists($arg, false))
225 {
226 $result = 'c:' . $arg;
227 }
228 elseif (interface_exists($arg, false))
229 {
230 $result = 'i:' . $arg;
231 }
232 else
233 {
234 if (mb_strlen($arg) > $this->argMaxChars)
235 {
236 $result = '"' . mb_substr($arg, 0, $this->argMaxChars / 2) . '...' . mb_substr($arg, -$this->argMaxChars / 2) . '" (' . mb_strlen($arg) . ')';
237 }
238 else
239 {
240 $result = '"' . $arg . '"';
241 }
242 }
243 break;
244 case 'array':
245 if (is_callable($arg, false, $callableName))
246 {
247 $result = 'fa:' . $callableName;
248 }
249 else
250 {
251 $result = 'array(' . count($arg) . ')';
252 }
253 break;
254 case 'object':
255 $result = '[' . get_class($arg) . ']';
256 break;
257 case 'resource':
258 $result = 'r:' . get_resource_type($arg);
259 break;
260 default:
261 $result = 'unknown type';
262 break;
263 }
264
265 return str_replace("\n", '\n', $result);
266 }
267
273 protected function formatDate(Type\Date $dateTime): string
274 {
275 return $dateTime->format('Y-m-d H:i:s');
276 }
277
283 protected function formatMixed($value): string
284 {
285 return var_export($value, true);
286 }
287
288 public static function severityToString($severity)
289 {
290 switch ($severity)
291 {
292 case 1:
293 return 'E_ERROR';
294 case 2:
295 return 'E_WARNING';
296 case 4:
297 return 'E_PARSE';
298 case 8:
299 return 'E_NOTICE';
300 case 16:
301 return 'E_CORE_ERROR';
302 case 32:
303 return 'E_CORE_WARNING';
304 case 64:
305 return 'E_COMPILE_ERROR';
306 case 128:
307 return 'E_COMPILE_WARNING';
308 case 256:
309 return 'E_USER_ERROR';
310 case 512:
311 return 'E_USER_WARNING';
312 case 1024:
313 return 'E_USER_NOTICE';
314 case 4096:
315 return 'E_RECOVERABLE_ERROR';
316 case 8192:
317 return 'E_DEPRECATED';
318 case 16384:
319 return 'E_USER_DEPRECATED';
320 case 30719:
321 return 'E_ALL';
322 default:
323 return 'UNKNOWN';
324 }
325 }
326}
static severityToString($severity)
Определения logformatter.php:288
formatArguments($args)
Определения logformatter.php:180
castToString($value, $placeholder=null)
Определения logformatter.php:68
formatException(\Throwable $exception)
Определения logformatter.php:111
formatTrace(array $trace)
Определения logformatter.php:141
formatMixed($value)
Определения logformatter.php:283
format($message, array $context=[])
Определения logformatter.php:32
formatDate(Type\Date $dateTime)
Определения logformatter.php:273
__construct($showArguments=false, $argMaxChars=30)
Определения logformatter.php:22
formatArgument($arg)
Определения logformatter.php:200
Определения date.php:9
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$context
Определения csv_new_setup.php:223
Определения arrayresult.php:2
Определения collection.php:2
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$val
Определения options.php:1793