|
20 | 20 |
|
21 | 21 | public function __construct(protected string $filePath) {} |
22 | 22 |
|
| 23 | + /** |
| 24 | + * {@inheritDoc} |
| 25 | + */ |
23 | 26 | public function search(string $query, bool $chunk = false): array |
24 | 27 | { |
25 | | - if (empty($query)) { |
| 28 | + if (empty($query) || ! file_exists($this->filePath)) { |
26 | 29 | return []; |
27 | 30 | } |
28 | 31 |
|
29 | 32 | $searchTerm = mb_strtolower($query); |
30 | 33 |
|
31 | | - if ($chunk) { |
32 | | - $chunkSize = (int) config('laravel-log-reader.file.chunk_size', 512 * 1024); // default 512KB |
33 | | - $handle = @fopen($this->filePath, 'r'); |
34 | | - |
35 | | - if (! $handle) { |
36 | | - return []; |
37 | | - } |
| 34 | + return $this->processFileChunks($chunk, function (array $logs) use ($searchTerm): array { |
| 35 | + return array_filter( |
| 36 | + $logs, |
| 37 | + fn(LogData $log) => str_contains(mb_strtolower($log->message ?? ''), $searchTerm) || |
| 38 | + str_contains(mb_strtolower($log->context ?? ''), $searchTerm) |
| 39 | + ); |
| 40 | + }); |
| 41 | + } |
38 | 42 |
|
39 | | - $results = []; |
40 | | - $buffer = ''; |
| 43 | + /** |
| 44 | + * {@inheritDoc} |
| 45 | + */ |
| 46 | + public function filter(array $filters = [], bool $chunk = false): array |
| 47 | + { |
| 48 | + if (! file_exists($this->filePath)) { |
| 49 | + return []; |
| 50 | + } |
41 | 51 |
|
42 | | - while (! feof($handle)) { |
43 | | - $buffer .= fread($handle, $chunkSize); |
| 52 | + if (empty($filters)) { |
| 53 | + return $this->parseLogFile(); |
| 54 | + } |
44 | 55 |
|
45 | | - if (! feof($handle)) { |
46 | | - $lastNewLinePos = strrpos($buffer, PHP_EOL); |
| 56 | + return $this->processFileChunks($chunk, function (array $logs) use ($filters): array { |
| 57 | + foreach ($filters as $key => $value) { |
| 58 | + $logs = array_filter($logs, fn(LogData $log) => $this->matchesFilter($log, $key, $value)); |
| 59 | + } |
47 | 60 |
|
48 | | - if ($lastNewLinePos === false) { |
49 | | - // no newline yet — read more before parsing |
50 | | - continue; |
51 | | - } |
| 61 | + return $logs; |
| 62 | + }); |
| 63 | + } |
52 | 64 |
|
53 | | - $contentChunk = substr($buffer, 0, $lastNewLinePos); |
54 | | - $buffer = substr($buffer, $lastNewLinePos + 1); |
55 | | - } else { |
56 | | - $contentChunk = $buffer; |
57 | | - $buffer = ''; |
58 | | - } |
| 65 | + /** |
| 66 | + * @param callable(array<LogData>) |
| 67 | + * |
| 68 | + * @return array<LogData> |
| 69 | + */ |
| 70 | + protected function processFileChunks(bool $chunk, callable $callback): array |
| 71 | + { |
| 72 | + if (! $chunk) { |
| 73 | + $logs = $this->parseLogFile(); |
59 | 74 |
|
60 | | - // Parse and filter this chunk |
61 | | - $logs = $this->convertToLogData($this->extractLogsFromContent($contentChunk)); |
| 75 | + return array_values($callback($logs)); |
| 76 | + } |
62 | 77 |
|
63 | | - $filtered = array_filter( |
64 | | - $logs, |
65 | | - fn(LogData $log) => str_contains(mb_strtolower($log->message ?? ''), $searchTerm) |
66 | | - || str_contains(mb_strtolower($log->context ?? ''), $searchTerm) |
67 | | - ); |
| 78 | + $chunkSize = (int) config('laravel-log-reader.file.chunk_size', 512 * 1024); |
| 79 | + $handle = @fopen($this->filePath, 'r'); |
68 | 80 |
|
69 | | - $results = array_merge($results, array_values($filtered)); |
70 | | - } |
| 81 | + if (! $handle) { |
| 82 | + return []; |
| 83 | + } |
71 | 84 |
|
72 | | - fclose($handle); |
| 85 | + $results = []; |
| 86 | + $buffer = ''; |
73 | 87 |
|
74 | | - return $results; |
75 | | - } |
| 88 | + while (! feof($handle)) { |
| 89 | + $buffer .= fread($handle, $chunkSize); |
76 | 90 |
|
77 | | - // Non-chunked (default) |
78 | | - $logs = $this->parseLogFile(); |
| 91 | + if (! feof($handle)) { |
| 92 | + $lastNewLinePos = strrpos($buffer, PHP_EOL); |
79 | 93 |
|
80 | | - return array_values(array_filter( |
81 | | - $logs, |
82 | | - fn(LogData $log) => str_contains(mb_strtolower($log->message ?? ''), $searchTerm) || |
83 | | - str_contains(mb_strtolower($log->context ?? ''), $searchTerm) |
84 | | - )); |
85 | | - } |
| 94 | + if ($lastNewLinePos === false) { |
| 95 | + continue; |
| 96 | + } |
86 | 97 |
|
87 | | - // todo add chunking/stream logic to filter method for both database filter method and here |
88 | | - public function filter(array $filters = []): array |
89 | | - { |
90 | | - $logs = $this->parseLogFile(); |
| 98 | + $contentChunk = substr($buffer, 0, $lastNewLinePos); |
| 99 | + $buffer = substr($buffer, $lastNewLinePos + 1); |
| 100 | + } else { |
| 101 | + $contentChunk = $buffer; |
| 102 | + $buffer = ''; |
| 103 | + } |
91 | 104 |
|
92 | | - if (empty($filters)) { |
93 | | - return $logs; |
| 105 | + $logs = $this->convertToLogData($this->extractLogsFromContent($contentChunk)); |
| 106 | + $filtered = $callback($logs); |
| 107 | + $results = array_merge($results, array_values($filtered)); |
94 | 108 | } |
95 | 109 |
|
96 | | - foreach ($filters as $key => $value) { |
97 | | - $logs = array_filter($logs, fn(LogData $log) => $this->matchesFilter($log, $key, $value)); |
98 | | - } |
| 110 | + fclose($handle); |
99 | 111 |
|
100 | | - return array_values($logs); |
| 112 | + return $results; |
101 | 113 | } |
102 | 114 |
|
103 | 115 | /** |
|
0 commit comments