简体   繁体   English

解析PHP错误日志问题 - 错误消息中的换行符(\\ n)

[英]parsing PHP error log issue – line-breaks (\n) inside the error messages

I am trying to parse PHP error log. 我试图解析PHP错误日志。 The issue is that breaking the file by \\n doesn't work. 问题是通过\\n中断文件不起作用。

explode(PHP_EOL, $log)

This doesn't work because there are some error messages that contain \\n by itself. 这不起作用,因为有一些错误消息本身包含\\n

How to break such file by lines then? 如何按行破解这样的文件呢?

Extract of the problematic log: 提取有问题的日志:

[04-Jan-2012 21:28:48] PHP Notice:  Use of undefined constant AY_FACEBOOK_TAB_URL - assumed 'AY_FACEBOOK_TAB_URL' in /var/www/[hidden]/default.layout.tpl.php on line 36
[04-Jan-2012 22:38:02] PHP Notice:  Use of undefined constant AY_FACEBOOK_TAB_URL - assumed 'AY_FACEBOOK_TAB_URL' in /var/www/[hidden]/default.layout.tpl.php on line 36
[04-Jan-2012 23:43:33] PHP Warning:  file_get_contents(https://graph.facebook.com/4294967295/picture?type=large): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error
 in /var/www/[hidden]/result.tpl.php on line 11
[04-Jan-2012 23:43:33] PHP Notice:  Undefined variable: image in /var/www/[hidden]/result.tpl.php on line 20

Notice the line break after HTTP/1.0 500 Internal Server Error . 注意HTTP/1.0 500 Internal Server Error后的换行符。

Use regular expression to match the start of the line. 使用正则表达式匹配行的开头。 It usually is a date/time (timestamp). 它通常是日期/时间(时间戳)。

This code works for me: 这段代码适合我:

<?php
    $content = file_get_contents('/var/log/php-log.log');

    var_dump(preg_split('/\[\d\d-\w{3}-\d{4}\s+\d\d:\d\d:\d\d\]/', $content));

But though i am splitting with the line-start pattern, the first result item would be empty. 但是,虽然我正在分割行开始模式,但第一个结果项将为空。

尝试使用"\\r\\n"并确保它们位于双引号之间。

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

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