|
1
|
|
|
<?php |
|
2
|
|
|
namespace nebula\component\debug\log\logger\filelogger; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* 多行附加属性 |
|
6
|
|
|
*/ |
|
7
|
|
|
trait AttachTrait |
|
8
|
|
|
{ |
|
9
|
|
|
public static function parameterToString($object, int $deep=2) |
|
10
|
|
|
{ |
|
11
|
|
|
if (is_null($object)) { |
|
12
|
|
|
return 'NULL'; |
|
13
|
|
|
} elseif (is_object($object)) { |
|
14
|
|
|
$objectName = get_class($object); |
|
15
|
|
|
$parameterString = ''; |
|
16
|
|
|
|
|
17
|
|
|
if ($deep > 0) { |
|
18
|
|
|
$vars = get_class_vars($objectName); |
|
19
|
|
|
foreach ($vars as $key => $value) { |
|
20
|
|
|
if (is_string($value) && strlen($value) > self::$strLength) { |
|
21
|
|
|
$parameterString .= $key.'='.json_encode(substr($value, 0, 80), JSON_UNESCAPED_UNICODE) .'...,'; |
|
22
|
|
|
} else { |
|
23
|
|
|
$parameterString .= $key.'='.self::parameterToString($value, $deep-1) .','; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
} else { |
|
27
|
|
|
$parameterString = '...'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $objectName.' {'.trim($parameterString, ',').'}'; |
|
31
|
|
|
} elseif (is_array($object)) { |
|
32
|
|
|
$parameterString = ''; |
|
33
|
|
|
if ($deep > 0) { |
|
34
|
|
|
foreach ($object as $key => $value) { |
|
35
|
|
|
if (is_string($value) && strlen($value) > self::$strLength) { |
|
36
|
|
|
$parameterString .= json_encode(substr($value, 0, 80), JSON_UNESCAPED_UNICODE) .'...,'; |
|
37
|
|
|
} else { |
|
38
|
|
|
$parameterString .= static::parameterToString($value, $deep-1) .','; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} else { |
|
42
|
|
|
$parameterString = '...'; |
|
43
|
|
|
} |
|
44
|
|
|
return '['.trim($parameterString, ',').']'; |
|
45
|
|
|
} |
|
46
|
|
|
return $object; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function printTrace(array $backtrace, bool $str=true, string $perfix='') |
|
50
|
|
|
{ |
|
51
|
|
|
$tracesConsole=[]; |
|
52
|
|
|
foreach ($backtrace as $trace) { |
|
53
|
|
|
$printDump = null; |
|
54
|
|
|
if (isset($trace['file'])) { |
|
55
|
|
|
$printDump=$trace['file'].':'.$trace['line']; |
|
56
|
|
|
} |
|
57
|
|
|
if (isset($trace['class'])) { |
|
58
|
|
|
$function = $trace['class'].$trace['type'].$trace['function']; |
|
59
|
|
|
} else { |
|
60
|
|
|
$function = $trace['function']; |
|
61
|
|
|
} |
|
62
|
|
|
$argsDump=''; |
|
63
|
|
|
if (!empty($trace['args'])) { |
|
64
|
|
|
foreach ($trace['args'] as $arg) { |
|
65
|
|
|
$argsDump.= self::parameterToString($arg) .','; |
|
66
|
|
|
} |
|
67
|
|
|
$argsDump = rtrim($argsDump, ','); |
|
68
|
|
|
} |
|
69
|
|
|
$printDump.=' '.$function.'('.$argsDump.')'; |
|
70
|
|
|
$tracesConsole[]=$printDump; |
|
71
|
|
|
} |
|
72
|
|
|
if ($str) { |
|
73
|
|
|
$str=''; |
|
74
|
|
|
foreach ($tracesConsole as $trace_info) { |
|
75
|
|
|
$str.=$perfix.preg_replace('/\n/', "\n".$perfix."\t", $trace_info).PHP_EOL; |
|
76
|
|
|
} |
|
77
|
|
|
return $str; |
|
78
|
|
|
} |
|
79
|
|
|
return $tracesConsole; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function dumpException(\Exception $e) { |
|
83
|
|
|
$dump = $e->getMessage() .PHP_EOL; |
|
84
|
|
|
$dump.= 'At: ' . $e->getFile().':'.$e->getLine().PHP_EOL; |
|
85
|
|
|
$dump.= static::printTrace($e->getTrace()); |
|
86
|
|
|
return $dump; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|