This repository was archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathLogOrganization.php
More file actions
122 lines (100 loc) · 3.68 KB
/
LogOrganization.php
File metadata and controls
122 lines (100 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
/**
* Helper class for generating and organizing log files.
*/
namespace Facebook\BusinessExtension\Helper;
class LogOrganization
{
// 4096 -- Good balance for tracking backwards
const BUFFER = 4096;
static $criticalLines = array();
public static function organizeLogs() {
$arrayOfFiles = array("var/log/debug.log", "var/log/cron.log", "var/log/magento.cron.log", "var/log/system.log");
$countCrit = 0;
$fp = null;
foreach($arrayOfFiles as $value) {
if (!file_exists($value)) {
continue;
}
$fp = fopen($value, 'r');
$pos = -2; // Skip final new line character (Set to -1 if not present)
$currentLine = '';
while (-1 !== fseek($fp, $pos, SEEK_END)) {
$char = fgetc($fp);
if ("\n" == $char) {
if (substr($currentLine, 0, 3) == "[20") {
$time = strtotime(substr($currentLine, 1, 20));
if ($time > strtotime('-7 day')) {
break;
}
// Saving only critical log entries with "Facebook" phrase
if (strpos($currentLine, "CRITICAL") !== false &&
strpos($currentLine, "Facebook") !== false) {
self::$criticalLines[] = $currentLine;
$countCrit++;
}
$currentLine = '';
if ($countCrit == 500) {
break;
}
}
} else {
$currentLine = $char . $currentLine;
}
$pos--;
}
if (substr($currentLine, 0, 3) == "[20") {
$time = strtotime(substr($currentLine, 1, 20));
if ($time > strtotime('-7 day')) {
break;
}
if (strpos($currentLine, "CRITICAL") !== false &&
strpos($currentLine, "Facebook") !== false) {
self::$criticalLines[] = $currentLine;
$countCrit++;
}
$currentLine = '';
}
$countCrit = 0;
}
if ($fp !== null) {
fclose($fp);
}
$amuLogs = self::tailCustom("var/log/facebook-business-extension.log", 100);
$amuLogsArr = explode("\n", $amuLogs);
self::$criticalLines = array_merge(self::$criticalLines, $amuLogsArr);
usort(self::$criticalLines, function ($x, $y) {
$t1 = strtotime(substr($x, 1, 19));
$t2 = strtotime(substr($y, 1, 19));
return ($t1 - $t2);
});
return self::$criticalLines;
}
public static function tailCustom($filepath, $lines) {
$f = fopen(BP . '/' . $filepath, "rb");
if ($f === false) {
return false;
}
fseek($f, -1, SEEK_END);
if (fread($f, 1) != "\n") {
$lines -= 1;
}
$output = '';
$chunk = '';
while (ftell($f) > 0 && $lines >= 0) {
$seek = min(ftell($f), self::BUFFER);
fseek($f, -$seek, SEEK_CUR);
$output = ($chunk = fread($f, $seek)) . $output;
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
$lines -= substr_count($chunk, "\n");
}
while ($lines++ < 0) {
$output = substr($output, strpos($output, "\n") + 1);
}
fclose($f);
return trim($output);
}
}