|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\framework\debug; |
|
4
|
|
|
|
|
5
|
|
|
use suda\framework\debug\log\LogLevel; |
|
6
|
|
|
use suda\framework\debug\log\LoggerTrait; |
|
7
|
|
|
use suda\framework\debug\attach\DumpTrait; |
|
8
|
|
|
use suda\framework\debug\attach\AttachTrait; |
|
9
|
|
|
use suda\framework\debug\log\LoggerInterface; |
|
10
|
|
|
use suda\framework\debug\attach\DumpInterface; |
|
11
|
|
|
use suda\framework\debug\log\LoggerAwareTrait; |
|
12
|
|
|
use suda\framework\debug\attach\AttachInterface; |
|
13
|
|
|
use suda\framework\debug\log\LoggerAwareInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Debug implements LoggerInterface, LoggerAwareInterface, DumpInterface, AttachInterface, ConfigInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use LoggerTrait, LoggerAwareTrait, DumpTrait, AttachTrait, ConfigTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* 忽略堆栈 |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $ignoreTraces = [__DIR__]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* 时间记录 |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $timeRecord; |
|
32
|
|
|
|
|
33
|
|
|
public function log(string $level, string $message, array $context = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$attribute = $this->getAttribute(); |
|
36
|
|
|
$attribute['message'] = $this->strtr($message, $context); |
|
37
|
|
|
$attribute['level'] = $level; |
|
38
|
|
|
$caller = new Caller(debug_backtrace(), $this->getIgnoreTraces()); |
|
39
|
|
|
$trace = $caller->getCallerTrace(); |
|
40
|
|
|
$attribute['file'] = $trace['file']; |
|
41
|
|
|
$attribute['line'] = $trace['line']; |
|
42
|
|
|
$attribute = $this->assignAttributes($attribute); |
|
43
|
|
|
$this->logger->log($level, $this->interpolate($this->getConfig('log-format'), $context, $attribute), []); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* 设置忽略前缀 |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getIgnoreTraces(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->ignoreTraces; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getDefaultConfig(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
|
|
'log-format' => '%time-format% - %memory-format% [%level%] %file%:%line% %message%', |
|
60
|
|
|
'start-time' => 0, |
|
61
|
|
|
'start-memory' => 0, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function strtr(string $message, array $context) |
|
66
|
|
|
{ |
|
67
|
|
|
$replace = []; |
|
68
|
|
|
foreach ($context as $key => $val) { |
|
69
|
|
|
$replace['{' . $key . '}'] = $val; |
|
70
|
|
|
} |
|
71
|
|
|
return strtr($message, $replace); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function assignAttributes(array $attribute): array |
|
75
|
|
|
{ |
|
76
|
|
|
$attribute['current-time'] = number_format(microtime(true), 4, '.', ''); |
|
77
|
|
|
$time = microtime(true) - $this->getConfig('start-time'); |
|
78
|
|
|
$memory = memory_get_usage() - $this->getConfig('start-memory'); |
|
79
|
|
|
$attribute['time-format'] = number_format($time, 10, '.', ''); |
|
80
|
|
|
$attribute['memory-format'] = $this->formatBytes($memory, 2); |
|
81
|
|
|
$attribute['memory'] = $memory; |
|
82
|
|
|
return $attribute; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function formatBytes(int $bytes, int $precision = 0) |
|
86
|
|
|
{ |
|
87
|
|
|
$human = ['B', 'KB', 'MB', 'GB', 'TB']; |
|
88
|
|
|
$bytes = max($bytes, 0); |
|
89
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
|
90
|
|
|
$pos = min($pow, count($human) - 1); |
|
91
|
|
|
$bytes /= (1 << (10 * $pos)); |
|
92
|
|
|
return round($bytes, $precision) . ' ' . $human[$pos]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function time(string $name, string $type = LogLevel::INFO) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->timeRecord[$name] = ['time' => microtime(true), 'level' => $type]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function timeEnd(string $name) |
|
101
|
|
|
{ |
|
102
|
|
|
if (array_key_exists($name, $this->timeRecord)) { |
|
103
|
|
|
$pass = microtime(true) - $this->timeRecord[$name]['time']; |
|
104
|
|
|
$this->log( |
|
105
|
|
|
$this->timeRecord[$name]['level'], |
|
106
|
|
|
sprintf("process %s cost %ss", $name, number_format($pass, 5)) |
|
107
|
|
|
); |
|
108
|
|
|
return $pass; |
|
109
|
|
|
} |
|
110
|
|
|
return 0; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $ignoreTraces |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setIgnoreTraces(array $ignoreTraces) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->ignoreTraces = $ignoreTraces; |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* 添加忽略路径 |
|
126
|
|
|
* @param string $path |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function addIgnorePath(string $path) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->ignoreTraces[] = $path; |
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|