简体   繁体   English

Bash grep php error_log过滤器致命错误

[英]Bash grep php error_log Fatal errors to file by filter


I have webserver with several web applications running (PHP). 我有运行多个Web应用程序(PHP)的Web服务器。 The configuration is set up the way, that php error_log is deleted at midnight (so the file lasts for 24 hrs only). 配置是通过以下方式设置的,即在午夜删除php error_log(因此该文件仅持续24小时)。

The thing is, I would like to log all Fatal errors into another file or database but only for specific web applications. 问题是,我想将所有致命错误记录到另一个文件或数据库中,但仅适用于特定的Web应用程序。 (there are about 20 running, 4 of them are mine). (大约有20个正在运行,其中4个是我的)。

I was thinking about creating a bash script, grepping the error_log file on "Fatal" and url of my applications, fetching the output to file and also remembering last line number of current error_log in separate cache file. 我正在考虑创建一个bash脚本,在我的应用程序的“致命”和URL上复制error_log文件,将输出提取到文件中,并且还记得当前error_log的最后一行在单独的缓存文件中。

I would then put the script to cron and execute it every few minutes (starting at the last line of the previous run). 然后,我将脚本放到cron并每隔几分钟执行一次(从上次运行的最后一行开始)。

The whole idea is a little messed up and I think it could be written efficiently, any ideas? 整个想法有些混乱,我认为可以有效地编写,有什么想法吗?

Writing a cron job seems OK if you can't configure this out of the box. 如果您不能立即进行配置,则编写cron作业似乎可以。 I don't know PHP well enough. 我不太了解PHP。 In java eg you can have the same log message go to several log files depending on criteria. 例如,在Java中,您可以根据条件将相同的日志消息转到多个日志文件。

But I would have your cron job do both the collecting of the fatal errors AND the deletion of the "last day's" log file. 但是,我希望您的cron工作既可以收集致命错误,也可以删除“最后一天”的日志文件。 This way you can suffice with a single run of this script, at midnight, and save yourself the complexity of knowing where you ended last time (and the chance you missed some errors that happened right before midnight). 这样,您可以在午夜运行一次该脚本就足够了,并节省了上一次知道您在哪里结束的复杂性(以及您错过了午夜之前发生的一些错误的机会)。 If the collection was OK, delete the old file, otherwise leave it for diagnosis and retry. 如果收集正常,请删除旧文件,否则将其保留以进行诊断并重试。 It saves you a bunch (24*60) calls to the script. 它为您节省了一堆(24 * 60)脚本调用。

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

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