Last active
September 5, 2024 11:02
-
-
Save svandragt/0bf6d63c3f61749652b3b21dce7a483c to your computer and use it in GitHub Desktop.
Top PHP log errors
This file contains 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 | |
// Read the contents of the log file into an array | |
$log_file = 'web1_php_errors.log'; | |
$log_entries = file($log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
// Filter log entries for the last 14 days | |
$current_date = new DateTime(); | |
$filtered_entries = array_filter($log_entries, function($entry) use ($current_date) { | |
// Extract the date part and convert it to a DateTime object | |
$entry_date_str = substr($entry, 1, 20); // Extracting the date part | |
$entry_date = DateTime::createFromFormat('d-M-Y H:i:s', $entry_date_str); | |
// Check if the entry date is within the last 14 days | |
if (!$entry_date) return false; | |
$interval = $current_date->diff($entry_date); | |
return $interval->days <= 14; | |
}); | |
// Strip the timestamp part from each filtered entry | |
$filtered_entries = array_map(function($entry) { | |
// Strip the timestamp part | |
$entry = preg_replace('/\[\d{2}-[A-Za-z]{3}-\d{4} \d{2}:\d{2}:\d{2} UTC\] /', '', $entry); | |
return $entry; | |
}, $filtered_entries); | |
// Count occurrences of each unique error | |
$error_counts = array_count_values($filtered_entries); | |
// Sort errors by occurrence count in descending order | |
arsort($error_counts); | |
// Get the top 10 errors | |
$top_errors = array_slice($error_counts, 0, 10); | |
// Output CSV data | |
$output_csv = fopen('report.csv', 'w'); | |
fputcsv($output_csv, ['Error', 'Count']); | |
foreach ($top_errors as $error => $count) { | |
fputcsv($output_csv, [$error, $count]); | |
} | |
fclose($output_csv); | |
echo "CSV file 'report.csv' has been generated successfully.". PHP_EOL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rich can show a table in the terminal