2016-02-29 65 views
4

那么,我试图解析PHP错误日志。因此,我建立了下面的类:如何解析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错误日志中的错误信息?

在此先感谢,干杯!

+0

为什么豆你需要解析PHP的错误文件?要找到你的代码的问题?你可以随时设置你自己的错误和excepion处理程序,这是你知道的首选方式。 –

+0

不,我正在为我的网站构建一个管理员仪表板,我只想要一个Web界面来监视我的错误日志。 – Ikari

+0

这就是我的想法。您需要为此构建您自己的错误处理程序,您可以将这些错误以您想要的格式保存在数据库或文件中。我将发布一个简单的解决方案,你从那里去。 –

回答

2

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

<?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; 
} 
} 
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语句,执行DB保存,或者只是使用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")); 
      } 
     } 
    } 
}