-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_logs.php
More file actions
76 lines (63 loc) · 1.77 KB
/
Copy pathexport_logs.php
File metadata and controls
76 lines (63 loc) · 1.77 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
<?php
$logFile = __DIR__ . '/api/storage/logs/aiops.log';
$outputFile = __DIR__ . '/logs.json';
if (!file_exists($logFile)) {
echo "Log file not found: $logFile\n";
exit(1);
}
$schemaKeys = [
'timestamp',
'request_id',
'method',
'path',
'status_code',
'latency_ms',
'client_ip',
'user_agent',
'query',
'payload_size_bytes',
'response_size_bytes',
'route_name',
'severity',
'build_version',
'host',
'error_category',
'error_message',
];
$logs = [];
$lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (!preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\].*request_log\s+(\{.*\})\s*$/', $line, $matches)) {
continue;
}
$decoded = json_decode($matches[2], true);
if (!is_array($decoded)) {
continue;
}
$normalized = array_fill_keys($schemaKeys, null);
$normalized['timestamp'] = $matches[1];
foreach ($schemaKeys as $key) {
if ($key === 'timestamp') {
continue;
}
if (array_key_exists($key, $decoded)) {
$normalized[$key] = $decoded[$key];
}
}
$logs[] = $normalized;
}
$totalLogs = count($logs);
$errorLogs = count(array_filter($logs, fn ($log) => ($log['severity'] ?? 'info') === 'error'));
echo "Total logs: {$totalLogs}\n";
echo "Error logs: {$errorLogs}\n";
if ($totalLogs < 1500) {
echo "WARNING: fewer than 1500 entries in aiops.log\n";
}
if ($errorLogs < 100) {
echo "WARNING: fewer than 100 error entries in aiops.log\n";
}
if ($totalLogs > 1500) {
$logs = array_slice($logs, -1500);
}
file_put_contents($outputFile, json_encode($logs, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
echo "Exported " . count($logs) . " logs to $outputFile\n";