|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Boost\Concerns; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Config; |
| 6 | + |
| 7 | +trait ReadsLogs |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Regular expression fragments and default chunk-window sizes used when |
| 11 | + * scanning log files. Declaring them once keeps every consumer in sync. |
| 12 | + */ |
| 13 | + private const TIMESTAMP_REGEX = '\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\]'; |
| 14 | + |
| 15 | + private const ENTRY_SPLIT_REGEX = '/(?='.self::TIMESTAMP_REGEX.')/'; |
| 16 | + |
| 17 | + private const ERROR_ENTRY_REGEX = '/^'.self::TIMESTAMP_REGEX.'.*\\.ERROR:/'; |
| 18 | + |
| 19 | + private const CHUNK_SIZE_START = 64 * 1024; // 64 kB |
| 20 | + |
| 21 | + private const CHUNK_SIZE_MAX = 1 * 1024 * 1024; // 1 MB |
| 22 | + |
| 23 | + /** |
| 24 | + * Resolve the current log file path based on Laravel's logging configuration. |
| 25 | + */ |
| 26 | + protected function resolveLogFilePath(): string |
| 27 | + { |
| 28 | + $channel = Config::get('logging.default'); |
| 29 | + $channelConfig = Config::get("logging.channels.{$channel}"); |
| 30 | + |
| 31 | + if (($channelConfig['driver'] ?? null) === 'daily') { |
| 32 | + return storage_path('logs/laravel-'.date('Y-m-d').'.log'); |
| 33 | + } |
| 34 | + |
| 35 | + return storage_path('logs/laravel.log'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Determine if the given line (or entry) is an ERROR log entry. |
| 40 | + */ |
| 41 | + protected function isErrorEntry(string $line): bool |
| 42 | + { |
| 43 | + return preg_match(self::ERROR_ENTRY_REGEX, $line) === 1; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Retrieve the last $count complete PSR-3 log entries from the log file using |
| 48 | + * chunked reading instead of character-by-character reverse scanning. |
| 49 | + * |
| 50 | + * @return string[] |
| 51 | + */ |
| 52 | + protected function readLastLogEntries(string $logFile, int $count): array |
| 53 | + { |
| 54 | + $chunkSize = self::CHUNK_SIZE_START; |
| 55 | + |
| 56 | + do { |
| 57 | + $entries = $this->scanLogChunkForEntries($logFile, $chunkSize); |
| 58 | + |
| 59 | + if (count($entries) >= $count || $chunkSize >= self::CHUNK_SIZE_MAX) { |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + $chunkSize *= 2; |
| 64 | + } while (true); |
| 65 | + |
| 66 | + return array_slice($entries, -$count); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return the most recent ERROR log entry, or null if none exists within the |
| 71 | + * inspected window. |
| 72 | + */ |
| 73 | + protected function readLastErrorEntry(string $logFile): ?string |
| 74 | + { |
| 75 | + $chunkSize = self::CHUNK_SIZE_START; |
| 76 | + |
| 77 | + do { |
| 78 | + $entries = $this->scanLogChunkForEntries($logFile, $chunkSize); |
| 79 | + |
| 80 | + for ($i = count($entries) - 1; $i >= 0; $i--) { |
| 81 | + if ($this->isErrorEntry($entries[$i])) { |
| 82 | + return trim($entries[$i]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if ($chunkSize >= self::CHUNK_SIZE_MAX) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + $chunkSize *= 2; |
| 91 | + } while (true); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Scan the last $chunkSize bytes of the log file and return an array of |
| 96 | + * complete log entries (oldest ➜ newest). |
| 97 | + * |
| 98 | + * @return string[] |
| 99 | + */ |
| 100 | + private function scanLogChunkForEntries(string $logFile, int $chunkSize): array |
| 101 | + { |
| 102 | + $fileSize = filesize($logFile); |
| 103 | + if ($fileSize === false) { |
| 104 | + return []; |
| 105 | + } |
| 106 | + |
| 107 | + $handle = fopen($logFile, 'r'); |
| 108 | + if (! $handle) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + $offset = max($fileSize - $chunkSize, 0); |
| 114 | + fseek($handle, $offset); |
| 115 | + |
| 116 | + // If we started mid-line, discard the partial line to align to next newline. |
| 117 | + if ($offset > 0) { |
| 118 | + fgets($handle); |
| 119 | + } |
| 120 | + |
| 121 | + $content = stream_get_contents($handle); |
| 122 | + if ($content === false) { |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | + // Split by beginning-of-entry look-ahead (PSR-3 timestamp pattern). |
| 127 | + $entries = preg_split(self::ENTRY_SPLIT_REGEX, $content, -1, PREG_SPLIT_NO_EMPTY); |
| 128 | + if (! $entries) { |
| 129 | + return []; |
| 130 | + } |
| 131 | + |
| 132 | + return $entries; // already in chronological order relative to chunk |
| 133 | + } finally { |
| 134 | + fclose($handle); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments