dongwei3172 2012-01-06 13:47
浏览 32
已采纳

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

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

explode(PHP_EOL, $log)

This doesn't work because there are some error messages that contain 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.

  • 写回答

2条回答 默认 最新

  • dq23171 2012-01-06 13:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)