|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\ServerInfo; |
| 11 | + |
| 12 | +use OCP\IConfig; |
| 13 | + |
| 14 | +class LogTailReader { |
| 15 | + private const READ_CHUNK = 96 * 1024; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + private IConfig $config, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Tail the Nextcloud JSON log and return the last $limit entries |
| 24 | + * with severity >= $minLevel (default: WARN = 2). Skips DEBUG/INFO. |
| 25 | + * |
| 26 | + * @return array{ |
| 27 | + * entries: list<array{time: string, level: int, app: string, message: string}>, |
| 28 | + * available: bool, |
| 29 | + * reason?: string |
| 30 | + * } |
| 31 | + */ |
| 32 | + public function recentErrors(int $limit = 8, int $minLevel = 2): array { |
| 33 | + $logType = $this->config->getSystemValue('log_type', 'file'); |
| 34 | + if ($logType !== 'file') { |
| 35 | + return ['entries' => [], 'available' => false, 'reason' => 'log_type_not_file']; |
| 36 | + } |
| 37 | + |
| 38 | + $path = $this->resolvePath(); |
| 39 | + if ($path === null || !is_readable($path)) { |
| 40 | + return ['entries' => [], 'available' => false, 'reason' => 'log_not_readable']; |
| 41 | + } |
| 42 | + |
| 43 | + $tail = $this->tailFile($path, self::READ_CHUNK); |
| 44 | + if ($tail === '') { |
| 45 | + return ['entries' => [], 'available' => true]; |
| 46 | + } |
| 47 | + |
| 48 | + $lines = explode("\n", $tail); |
| 49 | + $collected = []; |
| 50 | + // Iterate from newest to oldest. |
| 51 | + for ($i = count($lines) - 1; $i >= 0 && count($collected) < $limit; $i--) { |
| 52 | + $line = trim($lines[$i]); |
| 53 | + if ($line === '') { |
| 54 | + continue; |
| 55 | + } |
| 56 | + $decoded = json_decode($line, true); |
| 57 | + if (!is_array($decoded)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + $level = isset($decoded['level']) ? (int)$decoded['level'] : 0; |
| 61 | + if ($level < $minLevel) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + $collected[] = [ |
| 65 | + 'time' => (string)($decoded['time'] ?? ''), |
| 66 | + 'level' => $level, |
| 67 | + 'app' => (string)($decoded['app'] ?? ''), |
| 68 | + 'message' => $this->snippet((string)($decoded['message'] ?? '')), |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + return ['entries' => $collected, 'available' => true]; |
| 73 | + } |
| 74 | + |
| 75 | + private function resolvePath(): ?string { |
| 76 | + $dataDir = $this->config->getSystemValue('datadirectory', ''); |
| 77 | + $default = $dataDir !== '' ? rtrim($dataDir, '/') . '/nextcloud.log' : ''; |
| 78 | + $logFile = $this->config->getSystemValue('logfile', $default); |
| 79 | + if (!is_string($logFile) || $logFile === '') { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return $logFile; |
| 83 | + } |
| 84 | + |
| 85 | + private function tailFile(string $path, int $chunk): string { |
| 86 | + $size = @filesize($path); |
| 87 | + if ($size === false || $size === 0) { |
| 88 | + return ''; |
| 89 | + } |
| 90 | + $handle = @fopen($path, 'rb'); |
| 91 | + if ($handle === false) { |
| 92 | + return ''; |
| 93 | + } |
| 94 | + try { |
| 95 | + $readFrom = max(0, $size - $chunk); |
| 96 | + fseek($handle, $readFrom); |
| 97 | + $data = fread($handle, $chunk) ?: ''; |
| 98 | + // Drop the leading partial line so JSON parsing doesn't choke. |
| 99 | + if ($readFrom > 0) { |
| 100 | + $nl = strpos($data, "\n"); |
| 101 | + if ($nl !== false) { |
| 102 | + $data = substr($data, $nl + 1); |
| 103 | + } |
| 104 | + } |
| 105 | + return $data; |
| 106 | + } finally { |
| 107 | + fclose($handle); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private function snippet(string $msg, int $max = 200): string { |
| 112 | + $msg = trim($msg); |
| 113 | + if (function_exists('mb_strlen') && mb_strlen($msg) > $max) { |
| 114 | + return mb_substr($msg, 0, $max - 1) . '…'; |
| 115 | + } |
| 116 | + if (strlen($msg) > $max) { |
| 117 | + return substr($msg, 0, $max - 1) . '…'; |
| 118 | + } |
| 119 | + return $msg; |
| 120 | + } |
| 121 | +} |
0 commit comments