How can I parse Apache's error log in PHP?

后端 未结 6 1658
刺人心
刺人心 2021-01-04 00:38

I want to create a script that parses or makes sense of apache\'s error log to see what the most recent error was. I was wondering if anyone out there has something that doe

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 01:23

    There are a few things to consider first:

    1. Firstly, your PHP user may not have access to Apache's log files.
    2. Secondly, PHP and Apache aren't going to tell you where said log file is,
    3. Lastly, Apache log files can get quite large.

    However, if none of these apply, you can use the normal file reading commands to do it. The easiest way to get the last error is

    $contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES);
    if (is_array($contents)) {
        echo end($contents);
    }
    unset($contents);
    

    There's probably a better way of doing this that doesn't oink up memory, but I'll leave that as an exercise for the reader.

    One last comment: PHP also has an ini setting to redirect PHP errors to a log file: error_log = /path/to/error.log

    You can set this in httpd.conf or in an .htaccess file (if you have access to one) using the php_flag notation:

    php_flag error_log /web/mysite/logs/error.log
    

提交回复
热议问题