1+ <?php
2+ /**
3+ * User: dshibin
4+ * Date: 2019/12/30
5+ * Time: 17:29
6+ * Note: logging every log
7+ */
8+
9+ namespace DebugLog \Log ;
10+
11+ use DebugLog \Helper \Configs ;
12+
13+ class Log
14+ {
15+
16+ protected static $ instance = null ;
17+ protected $ data = [];
18+
19+ private function __construct ()
20+ {
21+ }
22+
23+ private function __clone ()
24+ {
25+ }
26+
27+ /**
28+ * 返回初始化实例类
29+ * @return Log|null
30+ */
31+ public static function getInstance (): ?Log
32+ {
33+ if (self ::$ instance instanceof self && !empty (self ::$ instance )) {
34+ return self ::$ instance ;
35+ }
36+ return self ::$ instance = new self ();
37+ }
38+
39+ /**
40+ * 记录对应的信息
41+ * @param string $name
42+ * @param $log
43+ * @param $st
44+ * @param $ed
45+ * @param $data
46+ * @return bool|null
47+ */
48+ public function Log (string $ name , $ log , $ st , $ ed , $ data ): ?bool
49+ {
50+ if ($ this ->_checkAuth () && $ this ->_checkLevel ($ name )) {
51+ $ this ->data [$ name ][] = [
52+ 'time ' => $ this ->_time ($ st , $ ed ),
53+ 'log ' => $ log ,
54+ 'data ' => $ data ,
55+ ];
56+ return true ;
57+ }
58+ return false ;
59+ }
60+
61+ /**
62+ * 记录堆栈信息
63+ * @return bool
64+ */
65+ public function LogStrace (): ?bool
66+ {
67+ foreach (array_slice (debug_backtrace (), -intval (Configs::getInstance ()->get ('debug_strace_num ' ))) as $ strace ) {
68+ if (!$ this ->Log ('strace ' , 'strace ' , 0 , 0 , $ strace )) {
69+ return false ;
70+ }
71+ }
72+ return true ;
73+ }
74+
75+ /**
76+ * 获取日志信息
77+ * @return array
78+ */
79+ public function getLog (?string $ logtype = null )
80+ {
81+ $ this ->LogStrace ();
82+ return (!empty ($ logtype ) && $ this ->data [$ logtype ]) ? $ this ->data [$ logtype ] : $ this ->data ;
83+ }
84+
85+ /**
86+ * 计算两个时间差
87+ * @param $st microtime返回的值
88+ * @param $ed microtime返回的值
89+ * @return float
90+ */
91+ protected function _time ($ st , $ ed )
92+ {
93+ if (is_numeric ($ st ) && is_numeric ($ ed )) {
94+ return $ ed - $ st ;
95+ }
96+ $ num = 1000 ;
97+ list ($ t1 , $ t2 ) = explode (" " , $ st );
98+ list ($ t3 , $ t4 ) = explode (" " , $ ed );
99+ $ st = round ((floatval ($ t1 * $ num ) + floatval ($ t2 * $ num )), 3 );
100+ $ ed = round ((floatval ($ t3 * $ num ) + floatval ($ t4 * $ num )), 3 );
101+ $ time = $ ed - $ st ;
102+ return round ($ time , 3 );
103+ }
104+
105+ /**
106+ * 校验是否要启用记录功能
107+ * @return bool
108+ */
109+ protected function _checkAuth (): ?bool
110+ {
111+ if (isset ($ _SERVER ['HTTP_USER_AGENT ' ]) && strpos ($ _SERVER ['HTTP_USER_AGENT ' ], Configs::getInstance ()->get ('debug_sign ' )) !== false && isset ($ _REQUEST [Configs::getInstance ()->get ('debug_level_sign ' )])) {
112+ return true ;
113+ }
114+ return false ;
115+ }
116+
117+ /**
118+ * 校验记录的等级信息
119+ * @param string $name
120+ * @return bool|null
121+ */
122+ protected function _checkLevel (string $ name ): ?bool
123+ {
124+ if (!isset ($ _REQUEST [Configs::getInstance ()->get ('debug_level_sign ' )])) {
125+ return false ;
126+ }
127+ $ debugLevelType = $ _REQUEST [Configs::getInstance ()->get ('debug_level_sign ' )];
128+ $ debugLevelArr = Configs::getInstance ()->get ('debug_level ' );
129+ if (!isset ($ debugLevelArr [$ debugLevelType ])) {
130+ return false ;
131+ }
132+ if (!in_array ($ name , $ debugLevelArr [$ debugLevelType ], true )) {
133+ return false ;
134+ }
135+ return true ;
136+ }
137+ }
0 commit comments