1. <?php
  2. /**
  3. * Batch Log File Fixer
  4. *
  5. * This script performs batch operations on log files based on hard-coded limits.
  6. */
  7. // Configuration - adjust these limits as needed
  8. $max_lines_to_process = 1000;
  9. $max_file_size_kb = 1024 * 10; // 10MB
  10. $fix_error_threshold = 5; // Number of errors before stopping
  11. // Function to process a single log file
  12. function processLogFile($filepath) {
  13. $lines = file($filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  14. $line_count = count($lines);
  15. if ($line_count > $max_lines_to_process) {
  16. echo "Skipping file: " . $filepath . " - Exceeds line limit ($max_lines_to_process)\n";
  17. return false; // Indicate skipping
  18. }
  19. $file_size_kb = filesize($filepath) / 1024;
  20. if ($file_size_kb > $max_file_size_kb) {
  21. echo "Skipping file: " . $filepath . " - Exceeds file size limit ($max_file_size_kb KB)\n";
  22. return false; // Indicate skipping
  23. }
  24. // Perform your fix operations here. Replace this with your actual logic.
  25. $errors = 0;
  26. foreach ($lines as $line) {
  27. // Example fix: Remove lines containing "ERROR"
  28. if (strpos($line, 'ERROR') !== false) {
  29. $errors++;
  30. //echo "Found error line: " . $line . "\n"; // Debugging
  31. }
  32. }
  33. if ($errors > $fix_error_threshold) {
  34. echo "Aborting processing of file: " . $filepath . " - Too many errors ($errors)\n";
  35. return false; // Indicate abort
  36. }
  37. // Example fix: Add a timestamp to each line.
  38. foreach($lines as &$line){
  39. $line = date('Y-m-d H:i:s') . " " . $line;
  40. }
  41. unset($line); // Break the reference
  42. // Save the modified file (optional)
  43. file_put_contents($filepath, implode("\n", $lines));
  44. echo "Processed file: " . $filepath . "\n";
  45. return true; // Indicate success
  46. }
  47. // Get a list of log files (adjust the path as needed)
  48. $log_files = glob('/path/to/logs/*.log'); // Replace with your log file directory
  49. if (empty($log_files)) {
  50. echo "No log files found.\n";
  51. } else {
  52. echo "Starting batch processing...\n";
  53. foreach ($log_files as $filepath) {
  54. if (processLogFile($filepath)) {
  55. // File processed successfully
  56. } else {
  57. // File processing failed (skipped or aborted)
  58. }
  59. }
  60. echo "Batch processing complete.\n";
  61. }
  62. ?>

Add your comment