How to analyze PHP error log?

Well, I tried to parse the PHP error log. So I built the following class:

<?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";
            }
        }
    }
}

(Well, maybe there are some bad practices in the code: -P)

For example, one mistake is [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.

In any case, I was able to extract the date and type of error. But I cannot find a way to parse the error message.

So my question is: how can I parse the error message from the PHP error log?

Thank Advance Cheers

+4
source share
3 answers

I'm a bit late, but my solution (it also supports stack traces):

<?php


use DateTime;
use DateTimeZone;

class ErrorLog {
private $logFilePath;

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

/**
 * Parses the PHP error log to an array.
 *
 * @return array
 */
public function getParsedLogFile() {
    $parsedLogs = [];
    $contents = file_get_contents($this->logFilePath);
    $lines = explode("\n", $contents);

    for ($currentLineNumber = 0; $currentLineNumber < count($lines); $currentLineNumber++) {
        $currentLine = trim($lines[$currentLineNumber]);

        // Normal error log line starts with the date & time in []
        if ('[' === substr($currentLine, 0, 1)) {
            // Get the datetime when the error occurred and convert it to berlin timezone
            $dateArr = [];
            preg_match('~^\[(.*?)\]~', $currentLine, $dateArr);
            $currentLine = str_replace($dateArr[0], '', $currentLine);
            $currentLine = trim($currentLine);
            $dateArr = explode(' ', $dateArr[1]);
            $dateTimeUtc = DateTime::createFromFormat('j-M-Y H:i:s', $dateArr[0] . ' ' . $dateArr[1]);
            $dateTimeUtc = $dateTimeUtc->format('Y-m-d H:i:s');
            $errorDateTime = new DateTime($dateTimeUtc, new DateTimeZone('UTC'));
            $errorDateTime->setTimezone(new DateTimeZone('Europe/Berlin'));
            $errorDateTime = $errorDateTime->format('Y-m-d H:i:s');

            // Get the type of the error
            $errorType = '';
            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';
            }

            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;

            for ($currentLineNumber++; $currentLineNumber < count($lines); $currentLineNumber++) {
                $currentLine = trim($lines[$currentLineNumber]);

                // If the current line is a stack trace line
                if ('#' === substr($currentLine, 0, 1)) {
                    $parsedLogsLastKey = end(array_keys($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;
                }
            }
        }
    }

    return $parsedLogs;
}
}
+2
source

Exception Error 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 , DB PHP error_log, .

+1

, , , , ... , ...

, , ERROR_TYPE, , ... ...

Update:

* 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"));
            }
        }
    }
}
0

All Articles