| 1 |
<?php |
| 2 |
class ErrorLog { |
| 3 |
private $logFilePath; |
| 4 |
|
| 5 |
/** |
| 6 |
* ErrorLog constructor. |
| 7 |
* |
| 8 |
* @param string $logFilePath |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
$this->logFilePath = ini_get('error_log'); |
| 12 |
} |
| 13 |
|
| 14 |
public function getParsedLogFile() { |
| 15 |
$parsedLogs = []; |
| 16 |
$logFileHandle = explode("\n", file_get_contents($this->logFilePath)); |
| 17 |
|
| 18 |
foreach ($logFileHandle as $id => $currentLine) { |
| 19 |
// Normal error log line starts with the date & time in [] |
| 20 |
if ('[' === @$currentLine[0]) { |
| 21 |
// if (10000 === \count($parsedLogs)) { |
| 22 |
// return $parsedLogs; |
| 23 |
// } |
| 24 |
|
| 25 |
// Get the datetime when the error occurred and convert it to berlin timezone |
| 26 |
try { |
| 27 |
$dateArr = []; |
| 28 |
preg_match('~^\[(.*?)\]~', $currentLine, $dateArr); |
| 29 |
$currentLine = str_replace($dateArr[0], '', $currentLine); |
| 30 |
$currentLine = trim($currentLine); |
| 31 |
$errorDateTime = new DateTime($dateArr[1]); |
| 32 |
$errorDateTime->setTimezone(new DateTimeZone('Europe/Berlin')); |
| 33 |
$errorDateTime = $errorDateTime->format('Y-m-d H:i:s'); |
| 34 |
} catch (\Exception $e) { |
| 35 |
$errorDateTime = ''; |
| 36 |
} |
| 37 |
|
| 38 |
// Get the type of the error |
| 39 |
if (false !== strpos($currentLine, 'PHP Warning')) { |
| 40 |
$currentLine = str_replace('PHP Warning:', '', $currentLine); |
| 41 |
$currentLine = trim($currentLine); |
| 42 |
$errorType = 'WARNING'; |
| 43 |
} else if (false !== strpos($currentLine, 'PHP Notice')) { |
| 44 |
$currentLine = str_replace('PHP Notice:', '', $currentLine); |
| 45 |
$currentLine = trim($currentLine); |
| 46 |
$errorType = 'NOTICE'; |
| 47 |
} else if (false !== strpos($currentLine, 'PHP Fatal error')) { |
| 48 |
$currentLine = str_replace('PHP Fatal error:', '', $currentLine); |
| 49 |
$currentLine = trim($currentLine); |
| 50 |
$errorType = 'FATAL'; |
| 51 |
} else if (false !== strpos($currentLine, 'PHP Parse error')) { |
| 52 |
$currentLine = str_replace('PHP Parse error:', '', $currentLine); |
| 53 |
$currentLine = trim($currentLine); |
| 54 |
$errorType = 'SYNTAX'; |
| 55 |
} else if (false !== strpos($currentLine, 'PHP Exception')) { |
| 56 |
$currentLine = str_replace('PHP Exception:', '', $currentLine); |
| 57 |
$currentLine = trim($currentLine); |
| 58 |
$errorType = 'EXCEPTION'; |
| 59 |
} else { |
| 60 |
$errorType = 'UNKNOWN'; |
| 61 |
} |
| 62 |
|
| 63 |
if (false !== strpos($currentLine, ' on line ')) { |
| 64 |
$errorLine = explode(' on line ', $currentLine); |
| 65 |
$errorLine = trim($errorLine[1]); |
| 66 |
$currentLine = str_replace(' on line ' . $errorLine, '', $currentLine); |
| 67 |
} else { |
| 68 |
$errorLine = substr($currentLine, strrpos($currentLine, ':') + 1); |
| 69 |
$currentLine = str_replace(':' . $errorLine, '', $currentLine); |
| 70 |
} |
| 71 |
|
| 72 |
$errorFile = explode(' in /', $currentLine); |
| 73 |
$errorFile = '/' . trim($errorFile[1]); |
| 74 |
$currentLine = str_replace(' in ' . $errorFile, '', $currentLine); |
| 75 |
|
| 76 |
// The message of the error |
| 77 |
$errorMessage = trim($currentLine); |
| 78 |
|
| 79 |
$parsedLogs[] = [ |
| 80 |
'id' => $id+1, |
| 81 |
'dateTime' => $errorDateTime, |
| 82 |
'type' => $errorType, |
| 83 |
'file' => $errorFile, |
| 84 |
'line' => (int)$errorLine, |
| 85 |
'message' => $errorMessage, |
| 86 |
'stackTrace' => [] |
| 87 |
]; |
| 88 |
} // Stack trace beginning line |
| 89 |
else if ('Stack trace:' === $currentLine) { |
| 90 |
$stackTraceLineNumber = 0; |
| 91 |
|
| 92 |
foreach ($logFileHandle as $line) { |
| 93 |
$currentLine = str_replace(PHP_EOL, '', $line); |
| 94 |
|
| 95 |
// If the current line is a stack trace line |
| 96 |
if ('#' === $currentLine[0]) { |
| 97 |
$parsedLogsLastKey = key($parsedLogs); |
| 98 |
$currentLine = str_replace('#' . $stackTraceLineNumber, '', $currentLine); |
| 99 |
$parsedLogs[$parsedLogsLastKey]['stackTrace'][] = trim($currentLine); |
| 100 |
|
| 101 |
$stackTraceLineNumber++; |
| 102 |
} // If the current line is the last stack trace ('thrown in...') |
| 103 |
else { |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $parsedLogs; |
| 111 |
} |
| 112 |
} |