-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathLogTailReader.php
More file actions
69 lines (59 loc) · 1.63 KB
/
LogTailReader.php
File metadata and controls
69 lines (59 loc) · 1.63 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\ServerInfo;
use OCP\IConfig;
use OCP\Log\IFileBased;
use OCP\Log\ILogFactory;
class LogTailReader {
public function __construct(
private IConfig $config,
private ILogFactory $logFactory,
) {
}
/**
* @return array{
* entries: list<array{time: string, level: int, app: string, message: string}>,
* available: bool,
* reason?: string
* }
*/
public function recentErrors(int $limit = 8, int $minLevel = 2): array {
$logType = $this->config->getSystemValue('log_type', 'file');
if ($logType !== 'file') {
return ['entries' => [], 'available' => false, 'reason' => 'log_type_not_file'];
}
$log = $this->logFactory->get('file');
if (!($log instanceof IFileBased)) {
return ['entries' => [], 'available' => false, 'reason' => 'log_not_readable'];
}
$raw = $log->getEntries($limit * 10);
$collected = [];
foreach ($raw as $entry) {
if (count($collected) >= $limit) {
break;
}
$level = (int)($entry['level'] ?? 0);
if ($level < $minLevel) {
continue;
}
$collected[] = [
'time' => (string)($entry['time'] ?? ''),
'level' => $level,
'app' => (string)($entry['app'] ?? ''),
'message' => $this->snippet((string)($entry['message'] ?? '')),
];
}
return ['entries' => $collected, 'available' => true];
}
private function snippet(string $msg, int $max = 200): string {
$msg = trim($msg);
if (mb_strlen($msg) > $max) {
return mb_substr($msg, 0, $max - 1) . '…';
}
return $msg;
}
}