Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m40s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m6s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m14s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m55s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m54s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m57s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m43s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
117 lines
3.5 KiB
PHP
117 lines
3.5 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
/**
|
||
* Die letzten Fehler aus dem Laravel-Protokoll (storage/logs) – für „Systemzustand“,
|
||
* damit die Administration Fehler ohne Serverzugang sieht und weitergeben kann.
|
||
*/
|
||
class ErrorLog
|
||
{
|
||
private const LEVELS = ['ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'];
|
||
|
||
/** Aus jeder Datei höchstens so viel vom Ende lesen */
|
||
private const TAIL_BYTES = 1_048_576;
|
||
|
||
public function __construct(private readonly ?string $directory = null) {}
|
||
|
||
/**
|
||
* @return list<array{time: string, level: string, message: string, exception: ?string, where: ?string, url: ?string, user: ?int}>
|
||
*/
|
||
public function recent(int $limit = 25): array
|
||
{
|
||
$entries = [];
|
||
|
||
foreach ($this->files() as $file) {
|
||
array_push($entries, ...$this->parse($this->tail($file)));
|
||
|
||
if (count($entries) >= $limit) {
|
||
break;
|
||
}
|
||
}
|
||
|
||
usort($entries, fn ($a, $b) => strcmp($b['time'], $a['time']));
|
||
|
||
return array_slice($entries, 0, $limit);
|
||
}
|
||
|
||
/**
|
||
* Neueste Protokolldatei (für den Download).
|
||
*/
|
||
public function latestFile(): ?string
|
||
{
|
||
return $this->files()[0] ?? null;
|
||
}
|
||
|
||
public function tail(string $file, int $bytes = self::TAIL_BYTES): string
|
||
{
|
||
$size = (int) @filesize($file);
|
||
$handle = @fopen($file, 'rb');
|
||
|
||
if (! $handle) {
|
||
return '';
|
||
}
|
||
|
||
fseek($handle, max(0, $size - $bytes));
|
||
$content = (string) stream_get_contents($handle);
|
||
fclose($handle);
|
||
|
||
return $content;
|
||
}
|
||
|
||
/**
|
||
* @return list<string> neueste zuerst
|
||
*/
|
||
private function files(): array
|
||
{
|
||
$files = glob(($this->directory ?? storage_path('logs')).'/laravel*.log') ?: [];
|
||
usort($files, fn ($a, $b) => filemtime($b) <=> filemtime($a));
|
||
|
||
return array_slice($files, 0, 3);
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
private function parse(string $content): array
|
||
{
|
||
preg_match_all('/^\[(\d{4}-\d{2}-\d{2}[ T][\d:.]+(?:[+-]\d{2}:\d{2})?)\] \w+\.([A-Z]+): (.*)$/m', $content, $matches, PREG_SET_ORDER);
|
||
$base = base_path().DIRECTORY_SEPARATOR;
|
||
$entries = [];
|
||
|
||
foreach ($matches as [, $time, $level, $line]) {
|
||
if (! in_array($level, self::LEVELS, true)) {
|
||
continue;
|
||
}
|
||
|
||
// Kontext steht als JSON hinter der Meldung
|
||
$message = $line;
|
||
$context = [];
|
||
|
||
if (($pos = strpos($line, ' {"')) !== false) {
|
||
$message = substr($line, 0, $pos);
|
||
$context = json_decode(substr($line, $pos + 1), true) ?: [];
|
||
}
|
||
|
||
$exception = null;
|
||
$where = null;
|
||
|
||
if (is_string($context['exception'] ?? null) && preg_match('/^\[object\] \(([^(]+)\(code: [^)]*\): .* at (.+):(\d+)\)/s', $context['exception'], $m)) {
|
||
$exception = class_basename(trim($m[1]));
|
||
$where = str_replace($base, '', $m[2]).':'.$m[3];
|
||
}
|
||
|
||
$entries[] = [
|
||
'time' => $time,
|
||
'level' => $level,
|
||
'message' => mb_substr(trim($message), 0, 500),
|
||
'exception' => $exception,
|
||
'where' => $where,
|
||
'url' => is_string($context['url'] ?? null) ? $context['url'] : null,
|
||
'user' => isset($context['user_id']) ? (int) $context['user_id'] : null,
|
||
];
|
||
}
|
||
|
||
return $entries;
|
||
}
|
||
}
|