简体   繁体   中英

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

I am trying to parse PHP error log. The issue is that breaking the file by \\n doesn't work.

explode(PHP_EOL, $log)

This doesn't work because there are some error messages that contain \\n by itself.

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 .

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"并确保它们位于双引号之间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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