Created
April 3, 2017 15:42
-
-
Save killpond/b3c04089f70ce3d901ab6db3b28464bb to your computer and use it in GitHub Desktop.
Parse Magento log files and return a combined count of each error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Parse error log file | |
echo '------------------------------- exception.log -------------------------------'; | |
$lines = file('exception.log'); | |
$error = ''; | |
$errors = array(); | |
$errors_count = array(); | |
foreach ($lines as $line_num => $line) { | |
$line = trim($line); | |
if (preg_match('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\+[0-9]{2}\:[0-9]{2} .+/si', $line) && !empty($error)) { | |
$code = base64_encode($error); | |
$errors[$code] = $error; | |
if (!isset($errors_count[$code])) { | |
$errors_count[$code] = 0; | |
} | |
$errors_count[$code]++; | |
$error = preg_replace('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\+[0-9]{2}\:[0-9]{2} (.+)/si', '$1', $line); | |
} else { | |
$error .= $line.PHP_EOL; | |
} | |
} | |
if (!empty($error)) { | |
$errors[base64_encode($error)] = $error; | |
} | |
foreach ($errors as $code => $error) { | |
echo $errors_count[$code].' => '.$error.PHP_EOL; | |
} | |
// Parse system log file | |
echo '------------------------------- system.log -------------------------------'; | |
$lines = file('system.log'); | |
$error = ''; | |
$errors = array(); | |
$errors_count = array(); | |
foreach ($lines as $line_num => $line) { | |
$line = trim($line); | |
if (preg_match('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\+[0-9]{2}\:[0-9]{2} .+/si', $line) && !empty($error)) { | |
$code = base64_encode($error); | |
$errors[$code] = $error; | |
if (!isset($errors_count[$code])) { | |
$errors_count[$code] = 0; | |
} | |
$errors_count[$code]++; | |
$error = preg_replace('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\+[0-9]{2}\:[0-9]{2} (.+)/si', '$1', $line); | |
} else { | |
$error .= $line.PHP_EOL; | |
} | |
} | |
if (!empty($error)) { | |
$errors[base64_encode($error)] = $error; | |
} | |
foreach ($errors as $code => $error) { | |
echo $errors_count[$code].' => '.$error.PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment