Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ErrorLogsChannel | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLogFileForIndex | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
getIndexForFilePath | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
parseDateTimeOfLine | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Logs\Utils; |
4 | |
5 | use Olz\Utils\WithUtilsTrait; |
6 | |
7 | class ErrorLogsChannel extends LogrotateLogsChannel { |
8 | use WithUtilsTrait; |
9 | |
10 | public static function getId(): string { |
11 | return 'error-logs'; |
12 | } |
13 | |
14 | public static function getName(): string { |
15 | return "Error Logs"; |
16 | } |
17 | |
18 | protected function getLogFileForIndex(int $index): LogFileInterface { |
19 | $log_name = 'error_log'; |
20 | $syslog_path = $this->envUtils()->getSyslogPath(); |
21 | $file_path = "{$syslog_path}{$log_name}.{$index}"; |
22 | if ($index === 0) { |
23 | $file_path = "{$syslog_path}{$log_name}"; |
24 | } |
25 | $index_path = "{$file_path}.index.json.gz"; |
26 | if (is_file($file_path)) { |
27 | return new PlainLogFile($file_path, $index_path); |
28 | } |
29 | if (is_file("{$file_path}.gz")) { |
30 | return new HybridLogFile("{$file_path}.gz", $file_path, $index_path); |
31 | } |
32 | throw new \Exception("No such file: {$file_path}"); |
33 | } |
34 | |
35 | protected function getIndexForFilePath(string $file_path): int { |
36 | $log_name = 'error_log'; |
37 | $syslog_path = $this->envUtils()->getSyslogPath(); |
38 | $esc_syslog_path = preg_quote($syslog_path, '/'); |
39 | $pattern = "/^{$esc_syslog_path}{$log_name}($|\\.(\\d+)$)/"; |
40 | $res = preg_match($pattern, $file_path, $matches); |
41 | if (!$res) { |
42 | throw new \Exception("Not an OLZ Log file path: {$file_path}"); |
43 | } |
44 | if ($matches[1] === '') { |
45 | return 0; |
46 | } |
47 | $index = intval($matches[2]) + 1; |
48 | if ($index < 0) { |
49 | throw new \Exception("Index is below 0. This should never happen, due to the regex."); |
50 | } |
51 | return $index; |
52 | } |
53 | |
54 | protected function parseDateTimeOfLine(string $line): ?\DateTime { |
55 | $res = preg_match('/(\w{3})\s+(\d{2})\s+(\d{2}\:\d{2}\:\d{2})\.\d+\s+(\d{4})/', $line, $matches); |
56 | $month_mapping = [ |
57 | 'jan' => '01', |
58 | 'feb' => '02', |
59 | 'mar' => '03', |
60 | 'apr' => '04', |
61 | 'may' => '05', |
62 | 'jun' => '06', |
63 | 'jul' => '07', |
64 | 'aug' => '08', |
65 | 'sep' => '09', |
66 | 'oct' => '10', |
67 | 'nov' => '11', |
68 | 'dec' => '12', |
69 | ]; |
70 | if (!$res) { |
71 | return null; |
72 | } |
73 | try { |
74 | $month = $month_mapping[strtolower($matches[1])]; |
75 | $date = "{$matches[4]}-{$month}-{$matches[2]}"; |
76 | return new \DateTime("{$date} {$matches[3]}"); |
77 | } catch (\Throwable $th) { |
78 | return null; |
79 | } |
80 | } |
81 | } |