baby_in_magento

Untitled

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