我有一个读取文本日志文件的脚本,查找包含单词"name“的行,然后将结果输出到页面。
日志文件包含许多不同的内容。日志快照如下:
10.07.2021 19:25:10: Peter-L: TCP heartbeat timeout
10.07.2021 19:25:14: Client 123.45.7.9:37000 connected
10.07.2021 19:25:14: Peter-L: Already connected
10.07.2021 19:25:14: Client 123.45.7.9:37000 disconnected: Connection closed by remote peer
10.07.2021 19:25:19: Peter-L: disconnected: Connection closed by remote peer
10.07.2021 19:25:20: Client 123.45.7.9:37000 connected
10.07.2021 19:25:20: Peter-L: Login OK from 123.45.7.9:37000 with protocol version 2.0
10.07.2021 19:25:20: Peter-L: Monitor TG#: [ 91 214 235 505 ]
10.07.2021 19:25:21: ### ReflectorClient::handleStateEvent: src=TetraLogic name=Sds:info msg=[{"last_activity":"1625909121","source":"Gateway2","tsi":"0022352","type":12}]
10.07.2021 19:26:43: ### ReflectorClient::handleStateEvent: src=TetraLogic name=QsoInfo:state msg=[{"call":"Steve-1","last_activity":"1625908748","source":"Gateway1","tsi":"0011223"}]我只对日志中的以下行感兴趣:
10.07.2021 19:26:43: ### ReflectorClient::handleStateEvent: src=TetraLogic name=QsoInfo:state msg=[{"call":"Steve-1","last_activity":"1625908748","source":"Gateway1","tsi":"0011223"}]当这一行在日志中出现时(它在日志中出现了多次),我想从日志中的该行中获取以下内容并将其发布到页面。日志中会有很多结果,所以我想过滤输出,使其只显示最后10个条目。
Date (10.07.2021) | Time (19:26:43) | Name (Steve-1)目前为止的脚本:
<?php
$lines = array();
$fopen = fopen('logfile.log', 'r');
while (!feof($fopen)) {
$line=fgets($fopen);
$line=trim($line);
$lines[]=$line;
}
fclose($fopen);
$finalOutput = array();
foreach ($lines as $string)
{
if (stripos(strtolower($string), 'name') !== false) {
$row = explode(" ", $string);
array_push($finalOutput,$row);
echo "<pre>";
print_r($finalOutput);
echo "</pre>";
}现在,我的输出如下所示:
Array
(
[0] => Array
(
[0] => 10.07.2021
[1] => 03:44:21:
[2] => ###
[3] => ReflectorClient::handleStateEvent:
[4] => src=SuperLogic
[5] => name=QsoInfo:state
[6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
)
)
Array
(
[0] => Array
(
[0] => 10.07.2021
[1] => 03:44:21:
[2] => ###
[3] => ReflectorClient::handleStateEvent:
[4] => src=SuperLogic
[5] => name=QsoInfo:state
[6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
)
[1] => Array
(
[0] => 10.07.2021
[1] => 13:20:26:
[2] => ###
[3] => ReflectorClient::handleStateEvent:
[4] => src=SuperLogic
[5] => name=QsoInfo:state
[6] => msg=[{"call":"James-2","last_activity":"1625887226","source":"gateway2","tsi":"0011244"}]
)
)
Array
(
[0] => Array
(
[0] => 10.07.2021
[1] => 03:44:21:
[2] => ###
[3] => ReflectorClient::handleStateEvent:
[4] => src=SuperLogic
[5] => name=QsoInfo:state
[6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
)发布于 2021-07-10 22:13:20
要只对最后10个结果进行排序,可以使用array_slice:
$lines= array_slice($lines, -10) ;
// return the 10 last lines of an array关于输出,"msg“是一个json字符串,我建议删除"msg["和],然后json_decode字符串:
$jsonResult = str_replace("msg[", "", $row[6]);
$jsonResult = str_replace("]", "", $jsonResult);
$jsonResult = json_decode($jsonResult) ;因此您可以使用和调用$jsonResult->call和$jsonResult->tsi。
发布于 2021-07-20 08:46:29
这是我使用的最后一段代码。它很混乱,但似乎确实起作用了:
<?php
$i = 0;
$lines=array();
$fopen = fopen('logfile.log', 'r');
while (!feof($fopen)) {
$line = fgets($fopen, 4096);
array_push($lines, $line);
}
rsort($lines);
fclose($fopen);
foreach ($lines as $string)
{
if (stripos(strtolower($string), 'name=QsoInfo') !== false) {
$string = preg_replace('!\s+!', ' ', $string);
$row = explode(" ", $string);
array_push($finalOutput,$row);
$pieces = explode(" ", $string);
$i++;
echo "<tr bgcolor=e6edf2><td height=20>";
echo $pieces[0]; // piece1
echo "</td><td>";
echo $pieces[1]; // piece2
echo "</td></tr>";
}
if ($i == 10) break;
}
?>https://stackoverflow.com/questions/68328010
复制相似问题