简体   繁体   English

解析日志文件并使用php存储在数组中

[英]parse a log file and store in a array using php

I am trying to parse a log file and store it in an array. 我正在尝试解析日志文件并将其存储在数组中。 New information is added to it every 1 hour (cronjob) and I want to make sure that after my script queries the DB only if I get new information(hostname) then send out an email otherwise we should not send out the email. 每隔1个小时(cronjob)就会向其中添加新信息,我想确保在脚本查询数据库之后,只有当我获得新信息(主机名)后再发送电子邮件,否则我们就不应该发送电子邮件。 I have the log file in the follwing format: 我有以下格式的日志文件:

Oct 31 13:45:01 Monitoring [info] Hostname : abc@abc.com
Oct 31 13:45:01 Monitoring [info] A count : 2195073
Oct 31 13:45:01 Monitoring [info] B count : 2191572
Oct 31 13:50:02 Monitoring [info] Hostname : abc@abc.com
Oct 31 13:50:02 Monitoring [info] A count : 2195073
Oct 31 13:50:02 Monitoring [info] B count : 2191572

I am looking to compare the hostname only and based on that comparison/logic will decide to send the email alert. 我希望仅比较主机名,并且基于该比较/逻辑,它将决定发送电子邮件警报。 Hence want to parse the above log file and store it in an array to perform the above action. 因此,要解析上面的日志文件并将其存储在数组中以执行上面的操作。 I will have to create a new function under a class to parse the log file. 我将必须在一个类下创建一个新函数来解析日志文件。

Can someone please help me out on how to create a function to parse the above log file ? 有人可以帮我解决如何创建上述日志文件的功能吗?

Thanks in advance! 提前致谢!

Might need a little more to go on here; 在这里可能还需要更多一点; what have you tried? 你尝试了什么? You're essentially just taking each line and parsing it for what you want. 本质上,您只是将每一行都解析为所需的内容。 There are tons of log parsers available. 有大量的日志解析器。

Looking for a PHP class to parse an access.log file 寻找一个PHP类来解析一个access.log文件

If you have log like this example, try : 如果您有类似此示例的日志,请尝试:

$str = "Oct 31 13:45:01 Monitoring [info] Hostname : abc@abc.com";

    $pos = strpos($str, 'Hostname');
    if($pos !== null){  
        echo substr($str, $pos+11); // hostname 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM