如何解析PHP错误日志?

6

我尝试解析PHP错误日志,因此我建立了以下类:

<?php
/**
 * Created by PhpStorm.
 * User: toton
 * Date: 2/29/2016
 * Time: 8:16 AM
 */
final class error_parser
{
    private $log_file_path;
    private $current_line;
    private $recent;

    /**
     * error_parser constructor.
     * Takes in the path of the error log file of PHP ERROR LOG FILE.
     * And another param for checking to get the direction to traverse the file.
     * @param string $log_file_path
     * @param bool $recent
     */
    public function __construct($log_file_path, $recent = true)
    {
        $this->log_file_path = $log_file_path;
        $this->recent = $recent;
        $this->_parse();
        return true;
    }

    /**
     * Parses the PHP ERROR LOG, and pushes an array with the following structure:
     * array(
     * "date" => {DATE},
     * "severity" => {SEVERITY},
     * "message" => {message},
     * "stack_trace" => array(each({STACK_TRACE})) || false;
     * );
     * to the main array.
     * !!!! IMPORTANT !!!!
     * STACK TRACE IS NOT SUPPORTED AT THIS MOMENT
     * TODO: IMPLEMENT STACK TRACE
     * MILESTONE: NEXT_MAJOR RELEASE
     */
    private function _parse() {
        $contents = file_get_contents($this->log_file_path);
        if(!$contents){
            throw new Exception("Log file does not exist.", 2);
        }
        $lines = explode("\n", $contents);
        if($this->recent) {
            $lines = array_reverse($lines);
        }
        for($this->current_line = 0; $this->current_line < count($lines); $this->current_line++) {
            parse_loop:
            $current_line = trim($lines[$this->current_line]);
            if(strlen($current_line) == 0) {
                //If the line is empty throw it to the dustbin.
                // SORRY, FOR THE GOTO.
                // GOD PLEASE FORGIVE ME!
                $this->current_line = $this->current_line + 1;
                goto parse_loop;
            }
            if($current_line[0] != "[") {
                // NOT SUPPORTING STACK TRACES AT THE MOMENT
                $this->current_line = $this->current_line + 1;
                goto parse_loop;
            }
            $dateArr = array();
            preg_match('~^\[(.*?)\]~', $current_line, $dateArr);
            $date = array(
                "date" => explode(" ", $dateArr[1])[0],
                "time" => explode(" ", $dateArr[1])[1]
            );
            $severity = "";
            if(strpos($current_line, "PHP Warning")) {
                $severity = "WARNING";
            } elseif(strpos($current_line, "PHP Notice")) {
                $severity = "NOTICE";
            } elseif(strpos($current_line, "PHP Fatal error")) {
                $severity = "FATAL";
            } elseif(strpos($current_line, "PHP Parse error")) {
                $severity = "SYNTAX_ERROR";
            } else {
                $severity = "UNIDENTIFIED_ERROR";
            }
        }
    }
}

代码中可能存在一些不良实践 :-P

例如,一个错误可能是这样的 - [28-Dec-2015 07:51:31 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_pspell.dll' - The specified module could not be found.

无论如何,我能够提取日期和错误类型。但我找不到一种解析错误消息的方法。

所以我的问题是,如何从PHP错误日志中解析出错误消息?

谢谢您的帮助,祝好!


为什么需要解析PHP的错误文件?是为了找到代码的问题吗?你总是可以设置自己的错误和异常处理程序,这是一种更好的方式。 - Muhammed
不,我正在为我的网站构建一个管理仪表板,我只想要一个Web界面来监视我的错误日志。 - Ikari
这正是我所想的。你需要为此构建自己的错误处理程序,可以将这些错误保存在数据库或文件中,以你想要的格式。我将发布一个简单的解决方案,你可以从那里开始。 - Muhammed
仍然有自己的错误处理程序是一个好主意。 - Muhammed
2
由于 PHP 错误日志文件是一致的,您不必使用 preg,只需使用 substr 并提取日期部分,之后 - 字符串的其余部分就是您的错误消息。 - Muhammed
显示剩余3条评论
3个回答

6

我有点晚了,但这是我的解决方案(它也支持堆栈跟踪):

<?php

use DateTime;
use DateTimeZone;

class ErrorLog {
    private $logFilePath;

    /**
     * ErrorLog constructor.
     *
     * @param string $logFilePath
     */
    public function __construct(string $logFilePath) {
        $this->logFilePath = $logFilePath;
    }

    /**
     * Parses the PHP error log to an array.
     *
     * @return \Generator
     */
    public function getParsedLogFile(): \Generator {
        $parsedLogs = [];
        $logFileHandle = fopen($this->logFilePath, 'rb');

        while (!feof($logFileHandle)) {
            $currentLine = str_replace(PHP_EOL, '', fgets($logFileHandle));

            // Normal error log line starts with the date & time in []
            if ('[' === $currentLine[0]) {
                if (10000 === \count($parsedLogs)) {
                    yield $parsedLogs;
                    $parsedLogs = [];
                }

                // Get the datetime when the error occurred and convert it to berlin timezone
                try {
                    $dateArr = [];
                    preg_match('~^\[(.*?)\]~', $currentLine, $dateArr);
                    $currentLine = str_replace($dateArr[0], '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorDateTime = new DateTime($dateArr[1]);
                    $errorDateTime->setTimezone(new DateTimeZone('Europe/Berlin'));
                    $errorDateTime = $errorDateTime->format('Y-m-d H:i:s');
                } catch (\Exception $e) {
                    $errorDateTime = '';
                }

                // Get the type of the error
                if (false !== strpos($currentLine, 'PHP Warning')) {
                    $currentLine = str_replace('PHP Warning:', '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorType = 'WARNING';
                } else if (false !== strpos($currentLine, 'PHP Notice')) {
                    $currentLine = str_replace('PHP Notice:', '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorType = 'NOTICE';
                } else if (false !== strpos($currentLine, 'PHP Fatal error')) {
                    $currentLine = str_replace('PHP Fatal error:', '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorType = 'FATAL';
                } else if (false !== strpos($currentLine, 'PHP Parse error')) {
                    $currentLine = str_replace('PHP Parse error:', '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorType = 'SYNTAX';
                } else if (false !== strpos($currentLine, 'PHP Exception')) {
                    $currentLine = str_replace('PHP Exception:', '', $currentLine);
                    $currentLine = trim($currentLine);
                    $errorType = 'EXCEPTION';
                } else {
                    $errorType = 'UNKNOWN';
                }

                if (false !== strpos($currentLine, ' on line ')) {
                    $errorLine = explode(' on line ', $currentLine);
                    $errorLine = trim($errorLine[1]);
                    $currentLine = str_replace(' on line ' . $errorLine, '', $currentLine);
                } else {
                    $errorLine = substr($currentLine, strrpos($currentLine, ':') + 1);
                    $currentLine = str_replace(':' . $errorLine, '', $currentLine);
                }

                $errorFile = explode(' in /', $currentLine);
                $errorFile = '/' . trim($errorFile[1]);
                $currentLine = str_replace(' in ' . $errorFile, '', $currentLine);

                // The message of the error
                $errorMessage = trim($currentLine);

                $parsedLogs[] = [
                    'dateTime'   => $errorDateTime,
                    'type'       => $errorType,
                    'file'       => $errorFile,
                    'line'       => (int)$errorLine,
                    'message'    => $errorMessage,
                    'stackTrace' => []
                ];
            } // Stack trace beginning line
            else if ('Stack trace:' === $currentLine) {
                $stackTraceLineNumber = 0;

                while (!feof($logFileHandle)) {
                    $currentLine = str_replace(PHP_EOL, '', fgets($logFileHandle));

                    // If the current line is a stack trace line
                    if ('#' === $currentLine[0]) {
                        $parsedLogsLastKey = key($parsedLogs);
                        $currentLine = str_replace('#' . $stackTraceLineNumber, '', $currentLine);
                        $parsedLogs[$parsedLogsLastKey]['stackTrace'][] = trim($currentLine);

                        $stackTraceLineNumber++;
                    } // If the current line is the last stack trace ('thrown in...')
                    else {
                        break;
                    }
                }
            }
        }

        yield $parsedLogs;
    }
}

1
您可以在PHP中定义自己的异常和错误处理程序,这样用户就不会看到错误,而是可以提供一个漂亮的错误消息告知他们发生了什么问题。
//Exception Handler
function my_exception_handler($exception) {
    echo "<p>App Exception {" , $exception->getMessage(), "}</p>\n";
}
\set_exception_handler('my_exception_handler');
//Error handler
function my_error_handler($errno, $errstr, $errfile, $errline){
    echo '<p style="color:darkred">App Error {'.$errno.', '.$errstr.'}</p>'."\n";
}
\set_error_handler('my_error_handler');

如果您没有使用任何命名空间,可以删除 set_error_.. 函数开头的 "\",或者保留它,以确保您的代码在自定义命名空间中正常工作。

自定义函数内部的 echo 语句,进行数据库保存,或者直接使用 PHP 的 error_log 函数来保存您的错误信息。


0

很奇怪,我找到了一种方法来做到这一点,我不知道这是否是最好的方法...但重要的是它能够工作...

所以我做的是,在解析和存储后,删除字符串中的日期和ERROR_TYPE等部分...最终我们只剩下错误消息...

更新:

*添加了返回错误的JSON和XML对象

这就是我的最终类:

<?php
/**
 * Created by PhpStorm.
 * User: toton
 * Date: 2/29/2016
 * Time: 8:16 AM
 */

/**
 * Class error_parser
 * Members:
 * @param $log_file_path - private
 * @param $current_line - private
 * @param $recent - private
 * @param $errors_array - private
 *
 * Methods:
 * @method \__construct() - public
 * @method \_parse() - private
 *
 * Function:
 * TO parse the PHP ERROR LOG and provide a readable and a programmable array, consisting of the errors!
 */
final class error_parser
{
    private $log_file_path;
    private $current_line;
    private $recent;
    private $errors_array = array();

    /**
     * error_parser constructor.
     * Takes in the path of the error log file of PHP ERROR LOG FILE.
     * And another param for checking to get the direction to traverse the file.
     * @param string $log_file_path
     * @param bool $recent
     */
    public function __construct($log_file_path, $recent = true)
    {
        $this->log_file_path = $log_file_path;
        $this->recent = $recent;
        $this->_parse();
        return true;
    }

    /**
     * Parses the PHP ERROR LOG, and pushes an array with the following structure:
     * array(
     * "date" => {DATE},
     * "severity" => {SEVERITY},
     * "message" => {message},
     * "stack_trace" => array(each({STACK_TRACE})) || false;
     * );
     * to the main array.
     * !!!! IMPORTANT !!!!
     * STACK TRACE IS NOT SUPPORTED AT THIS MOMENT
     * TODO: IMPLEMENT STACK TRACE
     * MILESTONE: NEXT_MAJOR RELEASE
     */
    private function _parse() {
        $contents = file_get_contents($this->log_file_path);
        if(!$contents){
            throw new Exception("Log file does not exist.", 2);
        }
        $lines = explode("\n", $contents);
        if($this->recent) {
            $lines = array_reverse($lines);
        }
        for($this->current_line = 0; $this->current_line < count($lines); $this->current_line++) {
            parse_loop:
            $current_line = trim($lines[$this->current_line]);
            if(strlen($current_line) == 0) {
                //If the line is empty throw it to the dustbin.
                // SORRY, FOR THE GOTO.
                // GOD PLEASE FORGIVE ME!
                $this->current_line = $this->current_line + 1;
                goto parse_loop;
            }
            if($current_line[0] != "[") {
                // NOT SUPPORTING STACK TRACES AT THE MOMENT
                $this->current_line = $this->current_line + 1;
                goto parse_loop;
            }
            $dateArr = array();
            preg_match('~^\[(.*?)\]~', $current_line, $dateArr);
            $current_line = str_replace($dateArr[0], "", $current_line);
            $current_line = trim($current_line);
            $date = array(
                "date" => explode(" ", $dateArr[1])[0],
                "time" => explode(" ", $dateArr[1])[1]
            );
            $severity = "";
            if(strpos($current_line, "PHP Warning") !== false) {
                $current_line = str_replace("PHP Warning:", "", $current_line);
                $current_line = trim($current_line);
                $severity = "WARNING";
            } elseif(strpos($current_line, "PHP Notice") !== false) {
                $current_line = str_replace("PHP Notice:", "", $current_line);
                $current_line = trim($current_line);
                $severity = "NOTICE";
            } elseif(strpos($current_line, "PHP Fatal error") !== false) {
                $current_line = str_replace("PHP Fatal error:", "", $current_line);
                $current_line = trim($current_line);
                $severity = "FATAL";
            } elseif(strpos($current_line, "PHP Parse error") !== false) {
                $current_line = str_replace("PHP Parse error:", "", $current_line);
                $current_line = trim($current_line);
                $severity = "SYNTAX_ERROR";
            } else {
                $severity = "UNIDENTIFIED_ERROR";
            }
            $message = $current_line;
            /* Final Array *//* Add nodes while creating them */
            $finalArray = array(
                "date" => $date,
                "severity" => $severity,
                "message" => $message
            );
            array_push($this->errors_array, $finalArray);
        }
    }

    /**
     * Function for returning the the error things in JSON format.
     * @return string <JSON STRING>
     */
    public function returnJson() {
        return json_encode($this->errors_array);
    }

    public function returnXml() {
        var_dump($this->errors_array);
        $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><errors></errors>");
        $this->_array_to_xml($xml, $this->errors_array);
        // By this time we would get a xml "STRING"
        return $xml->asXML();
    }

    /* Factory Function */
    /**
     * Converts an array to an xml-dcument
     * @param $data
     * @param $xml_data <SimpleXMLObject>
     */
    private function _array_to_xml(&$xml_data, $data) {
        foreach( $data as $key => $value ) {
            if( is_array($value) ) {
                if(is_numeric($key)){
                    $key = 'item' . $key;
                }
                $subnode = $xml_data->addChild($key);
                $this->_array_to_xml($subnode, $value);
            } else {
                $xml_data->addChild("$key",htmlspecialchars("$value"));
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接