baby_in_magento

Untitled

Dec 28th, 2023
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. <?php
  2. /**
  3. * Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
  4. */
  5.  
  6. /**
  7. * Helper class for generating and organizing log files.
  8. */
  9. namespace Facebook\BusinessExtension\Helper;
  10.  
  11. class LogOrganization
  12. {
  13. // 4096 -- Good balance for tracking backwards
  14. const BUFFER = 4096;
  15.  
  16. static $criticalLines = array();
  17.  
  18. public static function organizeLogs() {
  19. $arrayOfFiles = array("var/log/debug.log", "var/log/cron.log", "var/log/magento.cron.log", "var/log/system.log");
  20. $countCrit = 0;
  21.  
  22. foreach($arrayOfFiles as $value) {
  23. if (!file_exists($value)) {
  24. continue;
  25. }
  26.  
  27. $fp = fopen($value, 'r');
  28. $pos = -2; // Skip final new line character (Set to -1 if not present)
  29. $currentLine = '';
  30.  
  31. while (-1 !== fseek($fp, $pos, SEEK_END)) {
  32. $char = fgetc($fp);
  33. if ("\n" == $char) {
  34. if (substr($currentLine, 0, 3) == "[20") {
  35. $time = strtotime(substr($currentLine, 1, 20));
  36. if ($time > strtotime('-7 day')) {
  37. break;
  38. }
  39.  
  40. // Saving only critical log entries with "Facebook" phrase
  41. if (strpos($currentLine, "CRITICAL") !== false &&
  42. strpos($currentLine, "Facebook") !== false) {
  43. self::$criticalLines[] = $currentLine;
  44. $countCrit++;
  45. }
  46. $currentLine = '';
  47.  
  48. if ($countCrit == 500) {
  49. break;
  50. }
  51. }
  52. } else {
  53. $currentLine = $char . $currentLine;
  54. }
  55. $pos--;
  56. }
  57.  
  58. if (substr($currentLine, 0, 3) == "[20") {
  59. $time = strtotime(substr($currentLine, 1, 20));
  60. if ($time > strtotime('-7 day')) {
  61. break;
  62. }
  63.  
  64. if (strpos($currentLine, "CRITICAL") !== false &&
  65. strpos($currentLine, "Facebook") !== false) {
  66. self::$criticalLines[] = $currentLine;
  67. $countCrit++;
  68. }
  69. $currentLine = '';
  70. }
  71.  
  72. $countCrit = 0;
  73. fclose($fp);
  74. }
  75.  
  76.  
  77. $amuLogs = self::tailCustom("var/log/facebook-business-extension.log", 100);
  78. $amuLogsArr = explode("\n", $amuLogs);
  79. self::$criticalLines = array_merge(self::$criticalLines, $amuLogsArr);
  80.  
  81. usort(self::$criticalLines, function ($x, $y) {
  82. $t1 = strtotime(substr($x, 1, 19));
  83. $t2 = strtotime(substr($y, 1, 19));
  84.  
  85. return ($t1 - $t2);
  86. });
  87.  
  88. return self::$criticalLines;
  89. }
  90.  
  91. public static function tailCustom($filepath, $lines) {
  92. $f = fopen($filepath, "rb");
  93. if ($f === false) {
  94. return false;
  95. }
  96.  
  97. fseek($f, -1, SEEK_END);
  98. if (fread($f, 1) != "\n") {
  99. $lines -= 1;
  100. }
  101.  
  102. $output = '';
  103. $chunk = '';
  104.  
  105. while (ftell($f) > 0 && $lines >= 0) {
  106. $seek = min(ftell($f), self::BUFFER);
  107. fseek($f, -$seek, SEEK_CUR);
  108. $output = ($chunk = fread($f, $seek)) . $output;
  109. fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
  110. $lines -= substr_count($chunk, "\n");
  111. }
  112.  
  113. while ($lines++ < 0) {
  114. $output = substr($output, strpos($output, "\n") + 1);
  115. }
  116.  
  117. fclose($f);
  118. return trim($output);
  119. }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment