Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
logformatter.php
1<?php
2
10namespace Bitrix\Main\Diag;
11
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.
35 $message = $this->castToString($message);
36
37 if (!isset($context['delimiter']))
38 {
39 $context['delimiter'] = static::DELIMITER;
40 }
41
42 // 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.
43 $replace = [];
44 foreach ($context as $key => $val)
45 {
46 $replace['{' . $key . '}'] = $this->castToString($val, $key);
47 }
48
49 return strtr($message, $replace);
50 }
51
58 protected function castToString($value, $placeholder = null): string
59 {
60 if (!is_string($value))
61 {
62 if (is_object($value))
63 {
64 if ($placeholder == 'date' && $value instanceof Type\Date)
65 {
66 $value = $this->formatDate($value);
67 }
68 elseif ($placeholder == 'exception' && $value instanceof \Throwable)
69 {
70 $value = $this->formatException($value);
71 }
72 elseif (method_exists($value, '__toString'))
73 {
74 $value = (string)$value;
75 }
76 else
77 {
78 $value = $this->formatMixed($value);
79 }
80 }
81 else
82 {
83 if ($placeholder == 'trace' && is_array($value))
84 {
85 $value = $this->formatTrace($value);
86 }
87 else
88 {
89 $value = $this->formatMixed($value);
90 }
91 }
92 }
93 return $value;
94 }
95
101 protected function formatException(\Throwable $exception): string
102 {
103 $result = '[' . get_class($exception) . '] ';
104
105 if ($exception instanceof \ErrorException)
106 {
107 $result .= static::severityToString($exception->getSeverity());
108 }
109
110 $result .= "\n" . $exception->getMessage() . ' (' . $exception->getCode() . ')' . "\n";
111
112 if ($exception instanceof DB\SqlQueryException)
113 {
114 $result .= $exception->getQuery() . "\n";
115 }
116
117 $file = $exception->getFile();
118 if ($file)
119 {
120 $result .= $file . ':' . $exception->getLine() . "\n";
121 }
122
123 return $result;
124 }
125
131 protected function formatTrace(array $trace): string
132 {
133 $result = '';
134
135 foreach ($trace as $traceNum => $traceInfo)
136 {
137 $traceLine = '#'.$traceNum.': ';
138
139 if (isset($traceInfo['class']))
140 {
141 $traceLine .= $traceInfo['class'] . $traceInfo['type'];
142 }
143
144 if (isset($traceInfo['function']))
145 {
146 $traceLine .= $traceInfo['function'];
147 if(isset($traceInfo['args']))
148 {
149 $traceLine .= $this->formatArguments($traceInfo['args']);
150 }
151 }
152
153 $traceLine .= "\n\t";
154 if (isset($traceInfo['file']))
155 {
156 $traceLine .= $traceInfo['file'] . ':' . $traceInfo['line'];
157 }
158
159 $result .= $traceLine . "\n";
160 }
161
162 return $result;
163 }
164
170 public function formatArguments($args)
171 {
172 if ($args !== null)
173 {
174 $arguments = [];
175 foreach ($args as $arg)
176 {
177 $arguments[] = $this->formatArgument($arg);
178 }
179
180 return '(' . implode(', ', $arguments) . ')';
181 }
182 return '()';
183 }
184
190 public function formatArgument($arg)
191 {
192 if (!$this->showArguments)
193 {
194 return gettype($arg);
195 }
196
197 switch (gettype($arg))
198 {
199 case 'boolean':
200 $result = $arg ? 'true' : 'false';
201 break;
202 case 'NULL':
203 $result = 'null';
204 break;
205 case 'integer':
206 case 'double':
207 case 'float':
208 $result = (string)$arg;
209 break;
210 case 'string':
211 if (is_callable($arg, false, $callableName))
212 {
213 $result = 'fs:' . $callableName;
214 }
215 elseif (class_exists($arg, false))
216 {
217 $result = 'c:' . $arg;
218 }
219 elseif (interface_exists($arg, false))
220 {
221 $result = 'i:' . $arg;
222 }
223 else
224 {
225 if (mb_strlen($arg) > $this->argMaxChars)
226 {
227 $result = '"' . mb_substr($arg, 0, $this->argMaxChars / 2) . '...' . mb_substr($arg, -$this->argMaxChars / 2) . '" (' . mb_strlen($arg) . ')';
228 }
229 else
230 {
231 $result = '"' . $arg . '"';
232 }
233 }
234 break;
235 case 'array':
236 if (is_callable($arg, false, $callableName))
237 {
238 $result = 'fa:' . $callableName;
239 }
240 else
241 {
242 $result = 'array(' . count($arg) . ')';
243 }
244 break;
245 case 'object':
246 $result = '[' . get_class($arg) . ']';
247 break;
248 case 'resource':
249 $result = 'r:' . get_resource_type($arg);
250 break;
251 default:
252 $result = 'unknown type';
253 break;
254 }
255
256 return str_replace("\n", '\n', $result);
257 }
258
264 protected function formatDate(Type\Date $dateTime): string
265 {
266 return $dateTime->format('Y-m-d H:i:s');
267 }
268
274 protected function formatMixed($value): string
275 {
276 return var_export($value, true);
277 }
278
279 public static function severityToString($severity)
280 {
281 switch ($severity)
282 {
283 case 1:
284 return 'E_ERROR';
285 case 2:
286 return 'E_WARNING';
287 case 4:
288 return 'E_PARSE';
289 case 8:
290 return 'E_NOTICE';
291 case 16:
292 return 'E_CORE_ERROR';
293 case 32:
294 return 'E_CORE_WARNING';
295 case 64:
296 return 'E_COMPILE_ERROR';
297 case 128:
298 return 'E_COMPILE_WARNING';
299 case 256:
300 return 'E_USER_ERROR';
301 case 512:
302 return 'E_USER_WARNING';
303 case 1024:
304 return 'E_USER_NOTICE';
305 case 2048:
306 return 'E_STRICT';
307 case 4096:
308 return 'E_RECOVERABLE_ERROR';
309 case 8192:
310 return 'E_DEPRECATED';
311 case 16384:
312 return 'E_USER_DEPRECATED';
313 case 30719:
314 return 'E_ALL';
315 default:
316 return 'UNKNOWN';
317 }
318 }
319}
static severityToString($severity)
castToString($value, $placeholder=null)
formatException(\Throwable $exception)
format($message, array $context=[])
formatDate(Type\Date $dateTime)
__construct($showArguments=false, $argMaxChars=30)