-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlLogTool.php
More file actions
164 lines (152 loc) · 4.81 KB
/
Copy pathSqlLogTool.php
File metadata and controls
164 lines (152 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
header('content-type:text/html;charset=utf-8');
set_time_limit(0);
ini_set('memory_limit', -1);
defined('YW_LOG_FILE') ?: define('YW_LOG_FILE', $argv[0]);
if ($argv[1] == '-h') {
defined('YW_LOG_HELP') ?: define('YW_LOG_HELP', $argv[1]);
} else {
defined('YW_LOG_PATH') ?: define('YW_LOG_PATH', $argv[1]);
}
defined('YW_LOG_DIR') ?: define('YW_LOG_DIR', $argv[2]);
defined('YW_LOG_DAY') ?: define('YW_LOG_DAY', $argv[3]);
defined('YW_LOG_DAY_VAL') ?: define('YW_LOG_DAY_VAL', $argv[4]);
defined('YW_LOG_TIME') ?: define('YW_LOG_TIME', $argv[5]);
defined('YW_LOG_TIME_VAL') ?: define('YW_LOG_TIME_VAL', $argv[6]);
checkParamsValid();
$day = substr(YW_LOG_DAY_VAL, 0, 2);
$files = scanFile(YW_LOG_DIR, YW_LOG_DAY_VAL);
if (empty($files)) {
exit('没有' . $day . '号的日志');
}
$logResult = getLogResult($files, YW_LOG_TIME_VAL);
if (!empty($logResult)) {
print_r($logResult);
}
/**
* 获取日志结果
* @param array $files 文件地址
* @param int $timeParam 时间
* @return array $logResult
*/
function getLogResult ($files, $timeParam = 0) {
$separate = '--------------';
$logContent = '';
$logResult = [];
foreach ($files as $file) {
$f = fopen($file, "r");
$fileName = basename($file);
while (!feof($f)) {
$line = fgets($f);
if (stristr($line, $separate)) {
if (empty($logContent)) {
continue;
}
$log = handleLog($logContent, $timeParam);
if (!empty($log)) {
$logResult[$fileName][] = $log;
}
$logContent = '';
continue;
}
$logContent .= $line;
}
$log = handleLog($logContent, $timeParam);
$logContent = '';
if (!empty($log)) {
$logResult[$fileName][] = $log;
}
fclose($f);
}
return $logResult;
}
/**
* 匹配日志信息
* @param string $content 日志内容
* @param float $timeParam 时间
* @return array $sqlLogArr
*/
function handleLog($content, $timeParam) {
$preg = '/.*?\[.*?SQL.*?\].*?(SELECT[\s\S]*?)\s\[.*?RunTime:(.*?)s.*?\].*?/i';
preg_match_all($preg, $content, $match);
$pregAction = '/\[ (.*?) \] .*? (GET|POST) ([^ ]+)\n/i';
preg_match_all($pregAction, $content, $matchAction);
$sqlLogArr = [];
if (!empty($match[1])) {
foreach ($match[2] as $key => $value) {
if ($value >= $timeParam) {
if (!empty($matchAction[1][0])) {
$sqlLogArr['date'] = date('Y-m-d H:i:s', strtotime($matchAction[1][0]));
$sqlLogArr['action'] = $matchAction[3][0];
}
$sqlLogArr['info'][] = [
'sql' => $match[1][$key],
'time' => $match[2][$key],
];
}
}
}
return $sqlLogArr;
}
/**
* 查找文件夹下所有文件
* @param string $path 文件夹路径
* @param int $day 天数
* @return array $result
*/
function scanFile($path, $day = 0) {
global $result;
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
scanFile($path . '/' . $file);
} else {
if (empty($day)) {
$result[] = $path . '/' . basename($file);
}
if ($day == substr(basename($file), strrpos(basename($file), '.') - 2, 2)) {
$result[] = $path . '/' . basename($file);
}
}
}
}
return $result;
}
/**
* 判断参数是否合法
*/
function checkParamsValid() {
if (!file_exists(YW_LOG_FILE)) {
exit("Error: 执行文件路径错误");
}
if (defined('YW_LOG_HELP') && YW_LOG_HELP == '-h') {
$info = [
' e.g. php ./sqlLog.php -p path -d day -t time',
' -p 日志的目录路径',
' -d 指定文件的日期,查看所有输入0',
' -t 指定大于SQL的时间,查看所有输入0',
];
$content = implode("\n", $info);
printf("\n" . $content . "\n\n");
exit;
}
if (defined('YW_LOG_PATH') && YW_LOG_PATH != '-p') {
exit('Error: 第二个参数是 -p');
}
if (!is_dir(YW_LOG_DIR)) {
exit('Error: 目录不存在');
}
if (YW_LOG_DAY != '-d') {
exit('Error: 第四个参数必须是-d');
}
if (!is_numeric(YW_LOG_DAY_VAL)) {
exit('Error: 第五个参数是某天');
}
if (YW_LOG_TIME != '-t') {
exit('Error: 第六个参数必须是-t');
}
if (!is_numeric(YW_LOG_TIME_VAL)) {
exit('Error: 第七个参数是大于SQL的时间');
}
}