Skip to content

Commit 103cc89

Browse files
committed
debuglog init
1 parent 29e090d commit 103cc89

9 files changed

Lines changed: 524 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
/composer.lock

README.md

100644100755
Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
1-
# debuglog
2-
simple to debug log online
1+
## debuglog
2+
一个简单的php埋点记录插件,使用后设置对应的参数值,就能在线上追踪代码的运行的流程。
3+
###使用方法
4+
//引入记录装饰类
5+
use DebugLog\Log\LogDecorate;
6+
7+
//Db记录
8+
//记录Db运行时间
9+
$st = microtime();
10+
//mysql运行流程
11+
...
12+
LogDecorate::Db('select * from test',$st,microtime());
13+
14+
//redis记录
15+
//记录redis运行时间
16+
$st = microtime();
17+
//redis运行流程
18+
...
19+
LogDecorate::Redis('get key',$st,microtime());
20+
21+
//http记录
22+
//记录http运行时间
23+
$st = microtime();
24+
//http curl远程运行流程
25+
...
26+
LogDecorate::Http('curl baidu',$st,microtime());
27+
28+
//埋点info记录,可以记录自己想要的信息
29+
//记录info运行时间
30+
$st = microtime();
31+
//埋点运行流程
32+
...
33+
LogDecorate::Info('test_key',$st,microtime());
34+
35+
### 显示方法
36+
//引入记录装饰类
37+
use DebugLog\Log\LogDecorate;
38+
//通过show方法会直接显示出来
39+
LogDecorate::Show()
40+
//不过现在的框架很多都对显示做了过滤,也可以使用框架内置的显示方法,将记录的信息显示
41+
//获取记录的信息的方法
42+
LogDecorate::getLog()

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "dshibin/debuglog",
3+
"description": "a library of php debug",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.2"
7+
},
8+
"require-dev": {
9+
"php": ">=7.2"
10+
},
11+
"license": "Apache License 2.0",
12+
"authors": [
13+
{
14+
"name": "dshibin",
15+
"email": "275934879@qq.com"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"DebugLog\\": "src/"
21+
}
22+
},
23+
"minimum-stability": "dev"
24+
}

src/Conf/config.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* User: dshibin
4+
* Date: 2019/12/30
5+
* Time: 17:29
6+
* Note: the config of log
7+
*/
8+
9+
return [
10+
//自定义显示在请求头的信息,只有请求包含该信息,才会执行代码
11+
'debug_sign' => 'BBS_DEBUG_SHOW',
12+
//自定义在请求体get或者post里面的key值,接收对应的参数
13+
'debug_level_sign' => 'DEBUG_LEVEL',
14+
//显示等级 1 只显示轻量级信息 2 显示全部信息 3 显示全部信息+堆栈信息
15+
//信息类别有 db,redis,http,memcache,info,strace
16+
'debug_level' => [
17+
1 => ['db','redis'],
18+
2 => ['db','redis','http','info'],
19+
3 => ['db','redis','http','info','strace'],
20+
],
21+
//在请求里面返回对应的信息,false则隐藏
22+
'debug_show' => true,
23+
//显示类型,有json和html两种
24+
'debug_show_type' => 'html',
25+
//debug strace回流信息条数
26+
'debug_strace_num' => 10,
27+
];

src/Helper/Configs.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* User: dshibin
4+
* Date: 2019/8/27
5+
* Time: 19:44
6+
* Note: 配置设置 || 读取
7+
*/
8+
9+
namespace DebugLog\Helper;
10+
11+
class Configs
12+
{
13+
14+
private $config = [];
15+
16+
protected static $instance = null;
17+
18+
/**
19+
* 初始化配置文件类
20+
* Configs constructor.
21+
* @param $configs
22+
*/
23+
private function __construct()
24+
{
25+
$path = dirname(__DIR__) . '/Conf/';
26+
foreach (glob($path . '*.php') as $file) {
27+
$this->setArr(require_once $file);
28+
}
29+
}
30+
31+
private function __clone()
32+
{
33+
}
34+
35+
/**
36+
* 返回初始化实例类
37+
* @return Configs|null
38+
*/
39+
public static function getInstance(): ?Configs
40+
{
41+
if (self::$instance instanceof self && !empty(self::$instance)) {
42+
return self::$instance;
43+
}
44+
return self::$instance = new self();
45+
}
46+
47+
/**
48+
* 注入配置类到配置中
49+
* @param $class
50+
* @return array
51+
*/
52+
public function setClass($class): array
53+
{
54+
$class = new $class;
55+
$methods = get_class_methods($class);
56+
foreach ($methods as $method) {
57+
$class->$method();
58+
}
59+
if (isset($class->config) && !empty($class->config)) {
60+
return $this->config = array_merge($this->config, $class->config);
61+
}
62+
return $this->config;
63+
}
64+
65+
/**
66+
* 注入配置数组到配置中
67+
* @param $arr
68+
* @return array
69+
*/
70+
public function setArr($arr)
71+
{
72+
return $this->config = array_merge($this->config, $arr);
73+
}
74+
75+
/**
76+
* 获取配置库里面的值
77+
* @param string $name
78+
* @return array
79+
*/
80+
public function get(string ...$names)
81+
{
82+
$ret = $this->config;
83+
foreach ($names as $name) {
84+
$ret = $ret[$name] ?? [];
85+
if (empty($ret)) {
86+
break;
87+
}
88+
}
89+
return empty($ret) ? '' : $ret;
90+
}
91+
92+
/**
93+
* 获取配置库里面的值
94+
* @param string $name
95+
* @return array
96+
*/
97+
public function getAll(): array
98+
{
99+
return $this->config;
100+
}
101+
}

src/Helper/Responce.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* User: dshibin
4+
* Date: 2019/8/27
5+
* Time: 19:44
6+
* Note: 输出日志信息
7+
*/
8+
9+
namespace DebugLog\Helper;
10+
11+
class Responce
12+
{
13+
/**
14+
* 输出json字符串
15+
* @param $data
16+
*/
17+
public static function json($data)
18+
{
19+
if ($data && Configs::getInstance()->get('debug_show')) {
20+
echo json_encode($data, JSON_UNESCAPED_UNICODE);
21+
}
22+
}
23+
24+
/**
25+
* 输出html格式列表
26+
* @param $data
27+
*/
28+
public static function html($data)
29+
{
30+
if ($data && Configs::getInstance()->get('debug_show')) {
31+
$alltotal = 0;
32+
$str = '';
33+
foreach ($data as $key => $val) {
34+
$html = '';
35+
$total = $num = 0;
36+
foreach ($val as $k => $v) {
37+
++$num;
38+
$total += $v['time'];
39+
$alltotal += $v['time'];
40+
$html .= "<li>" . $v['time'] . " ms : " . $v['log'] . ' ' . (empty($v['data']) ? ' ' : json_encode($v['data'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) . " </li>" . PHP_EOL;
41+
}
42+
$str .= "<li><b style='font-size:18px;'>{$key} total count is {$num} , total time is {$total} ms</b></li>" . PHP_EOL . $html;
43+
}
44+
$str = "<ul style='margin:30px 45px'><li><b style='font-size:18px;'>DebugLog showViews.total process time : {$alltotal} ms</b></li>" . PHP_EOL . $str . '</ul><br>';
45+
echo $str;
46+
}
47+
}
48+
}

src/Log/Log.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)