php - 读取并过滤文本文件,然后将最后 10 个结果写入页面
问题描述
我有一个脚本,它读取文本日志文件,查找包含单词“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"}]
)
解决方案
要仅对最后 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) ;
所以你可以使用 and 调用$jsonResult->call
and $jsonResult->tsi
。
推荐阅读
- java - Hashmap 非空元组 vavr
- tsql - 连接两个表时需要多个条件的真假答案
- delphi - 为什么CopyRect在delphi 10.3中翻转了第二张图片?
- python - 如何在 Cython 中返回 C++ 包装的对象
- java - 如何为算术计算java解析器编写递归while循环
- c# - MSTest:测试分组
- x509certificate - 如何在 Corda 中手动生成网络参数
- matrix - CPLEX 无法从 Excel 文件中读取数据,因为矩阵太大?
- hadoop - 如何将 presto 集群集成到 hadoop 集群?
- javascript - 意外的 redux 操作结果