-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy2log
executable file
·243 lines (228 loc) · 7.05 KB
/
y2log
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env php
<?php
/**
*
*/
class Search{
/**
* %time%-100,100的单位
* d:day
* h:hour
* m:minute
* s:second
* @var string %time%-100,100的单位
*/
public $format = 's';
/**
* 查询日志文件
* 查询5分钟前的日志记录:
* ./yii log/search-log %time%-5 %time% {$file} --format=m
* 查询一天前的日志记录:
* ./yii log/search-log %time%-1 %time% {$file} --format=d
* 查询指定时间段的日志记录:
* ./yii log/search-log 2017-01-12\ 12:00:00 2017-01-13\ 13:00:00
* ./yii log/search-log 2017-01-12 2017-01-13
* example:
* kzcmd log/search-log %time%-25 %time% /var/www/html/hsehome2/app/console/runtime/logs/fortune-cron.log --format=m
* @param string $begin 开始时间
* @param string $end 结束时间
* @param string $file 日志文件路径
* @return string
*/
public function actionSearchLog($begin, $end, $file){
if(!file_exists($file)){
echo "$file 不存在\n";
exit();
}
// $file = '/var/www/html/hsehome2/app/console/runtime/logs/fortune-cron.log';
$time = $this->getTimes($file);
if(preg_match('/%time%([+\-]*[0-9]*)/', $begin, $matches)){
$begin = time() + $this->getOffset($this->format, $matches[1]);
}else{
$begin = strtotime($begin);
}
if(preg_match('/%time%([+\-]*[0-9]*)/', $end, $matches)){
$end = time() + $this->getOffset($this->format, $matches[1]);
}else{
$end = strtotime($end);
}
list($beginPos, $endPos) = $this->searchByTime($time, $begin, $end);
if($beginPos > 0){
$content = '';
foreach($time as $lineNum => $item){
if($beginPos <= $lineNum && $lineNum <= $endPos){
$content .= $this->getTextFromFromFile($file, $item['line'], $item['line'] + $item['range'] + 1);
}
}
echo $content;
}
}
private function getOffset($format, $value){
switch ($format) {
case 'd':
return 3600*24*$value;
case 'h':
return 3600*$value;
case 'm':
return 60*$value;
case 's':
return $value;
default:
throw new \Exception('unsupported format'. $format);
break;
}
}
private function getTimes($file){
$h = fopen($file, 'rb');
$lineNum = 0;
$lastPos = -1;
$time = [];
$range = -1;
while(false !== ($line = fgets($h))){
$lineNum++;
if(-1 != $range){
$range++;
}
if(preg_match('/^([0-9\-]+\s[0-9:]+)/', $line, $matches)){
if(-1 != $lastPos){
$time[$lastPos]['range'] = $range - 1;
}
$lastPos = $lineNum;
$range = 0;
$time[$lineNum] = [
'time' => strtotime($matches[1]),
'line' => $lineNum
];
}
}
$time[$lastPos]['range'] = $range;
fclose($h);
return $time;
}
private function searchByTime($data, $begin, $end){
// 寻找begin
$beginPos = -1;
foreach ($data as $key => $item) {
if($item['time'] >= $begin){
$beginPos = $key;
break;
}
}
// 寻找end
$endPos = -1;
$lastPos = -1;
foreach($data as $key => $item){
if($item['time'] > $end){
$endPos = $lastPos;
break;
}else{
$lastPos = $key;
}
}
if(-1 == $endPos){
$endPos = $lastPos;
}
return [$beginPos, $endPos];
}
private function getTextFromFromFile($file, $startLine, $endLine){
$h = fopen($file, 'rb');
$string = "";
$lineNum = 0;
while(false !== ($line = fgets($h))){
$lineNum++;
if(($lineNum >= $startLine) && ($lineNum < $endLine)){
$string .= $line;
}elseif($lineNum >= $endLine){
break;
}
}
fclose($h);
return $string;
}
}
class Request
{
private $_params;
/**
* Returns the command line arguments.
* @return array the command line arguments. It does not include the entry script name.
*/
public function getParams()
{
if ($this->_params === null) {
if (isset($_SERVER['argv'])) {
$this->_params = $_SERVER['argv'];
array_shift($this->_params);
} else {
$this->_params = [];
}
}
return $this->_params;
}
/**
* Sets the command line arguments.
* @param array $params the command line arguments
*/
public function setParams($params)
{
$this->_params = $params;
}
/**
* Resolves the current request into a route and the associated parameters.
* @return array the first element is the route, and the second is the associated parameters.
*/
public function resolve()
{
$rawParams = $this->getParams();
if (isset($rawParams[0])) {
$route = $rawParams[0];
array_shift($rawParams);
} else {
$route = '';
}
$params = [];
foreach ($rawParams as $param) {
if (preg_match('/^--(\w+)(?:=(.*))?$/', $param, $matches)) {
$name = $matches[1];
if ($name !== 'appconfig') {
$params[$name] = isset($matches[2]) ? $matches[2] : true;
}
} elseif (preg_match('/^-(\w+)(?:=(.*))?$/', $param, $matches)) {
$name = $matches[1];
$params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
} else {
$params[] = $param;
}
}
return [$route, $params];
}
}
date_default_timezone_set("Asia/Shanghai");
list($route, $params) = (new \Request())->resolve();
$search = new Search();
if(!empty($params['format'])){
$search->format = $params['format'];
unset($params['format']);
}
if('search' == $route){
call_user_func_array([$search, 'actionSearchLog'], $params);
}elseif(preg_match('/^[1-9][0-9]+$/', $route)){
call_user_func_array([$search, 'actionSearchLog'], [
'%time%-'.$route,
'%time%',
$params[0]
]);
}else{
echo "usage:\n";
echo "php ./yii2log.php search %time%-{value} %time% {logfile} --format={unit}\n";
echo "php ./yii2log.php {value} {logfile} --format={unit}\n";
echo "\n";
echo "map:\n";
echo "format:s, m, h, d\n";
echo "\n";
echo "example:\n";
echo "search log record in 1 minutes:\n";
echo "\t php ./yii2log.php search %time%-1 %time% ./log.txt --format=m\n";
echo "quickhand:\n";
echo "\t php ./yii2log.php 1 ./log.txt --format=m\n";
}