1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LogViewer\Helpers; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LogViewer\Utilities\LogLevels; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LogParser |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LogParser |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Constants |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
const REGEX_DATE_PATTERN = '\d{4}(-\d{2}){2}'; |
23
|
|
|
const REGEX_TIME_PATTERN = '\d{2}(:\d{2}){2}'; |
24
|
|
|
const REGEX_DATETIME_PATTERN = self::REGEX_DATE_PATTERN.' '.self::REGEX_TIME_PATTERN; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Properties |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Parsed data. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected static $parsed = []; |
37
|
|
|
|
38
|
|
|
/* ----------------------------------------------------------------- |
39
|
|
|
| Main Methods |
40
|
|
|
| ----------------------------------------------------------------- |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Parse file content. |
45
|
|
|
* |
46
|
|
|
* @param string $raw |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
378 |
|
public static function parse($raw) |
51
|
|
|
{ |
52
|
378 |
|
static::$parsed = []; |
53
|
378 |
|
list($headings, $data) = static::parseRawData($raw); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// @codeCoverageIgnoreStart |
56
|
|
|
if ( ! is_array($headings)) { |
57
|
|
|
return static::$parsed; |
58
|
|
|
} |
59
|
|
|
// @codeCoverageIgnoreEnd |
60
|
|
|
|
61
|
378 |
|
foreach ($headings as $heading) { |
62
|
378 |
|
for ($i = 0, $j = count($heading); $i < $j; $i++) { |
63
|
378 |
|
static::populateEntries($heading, $data, $i); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
|
67
|
378 |
|
unset($headings, $data); |
68
|
|
|
|
69
|
378 |
|
return array_reverse(static::$parsed); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* ----------------------------------------------------------------- |
73
|
|
|
| Other Methods |
74
|
|
|
| ----------------------------------------------------------------- |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Extract the date. |
79
|
|
|
* |
80
|
|
|
* @param string $string |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
390 |
|
public static function extractDate(string $string): string |
85
|
|
|
{ |
86
|
390 |
|
return preg_replace('/.*('.self::REGEX_DATE_PATTERN.').*/', '$1', $string); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Parse raw data. |
91
|
|
|
* |
92
|
|
|
* @param string $raw |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
378 |
|
private static function parseRawData($raw) |
97
|
|
|
{ |
98
|
378 |
|
$pattern = '/\['.self::REGEX_DATETIME_PATTERN.'\].*/'; |
99
|
378 |
|
preg_match_all($pattern, $raw, $headings); |
100
|
378 |
|
$data = preg_split($pattern, $raw); |
101
|
|
|
|
102
|
378 |
|
if ($data[0] < 1) { |
103
|
378 |
|
$trash = array_shift($data); |
104
|
378 |
|
unset($trash); |
105
|
|
|
} |
106
|
|
|
|
107
|
378 |
|
return [$headings, $data]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Populate entries. |
112
|
|
|
* |
113
|
|
|
* @param array $heading |
114
|
|
|
* @param array $data |
115
|
|
|
* @param int $key |
116
|
|
|
*/ |
117
|
378 |
|
private static function populateEntries($heading, $data, $key) |
118
|
|
|
{ |
119
|
378 |
|
foreach (LogLevels::all() as $level) { |
120
|
378 |
|
if (static::hasLogLevel($heading[$key], $level)) { |
|
|
|
|
121
|
|
|
static::$parsed[] = [ |
122
|
378 |
|
'level' => $level, |
123
|
378 |
|
'header' => $heading[$key], |
124
|
378 |
|
'stack' => $data[$key] |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
378 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if header has a log level. |
132
|
|
|
* |
133
|
|
|
* @param string $heading |
134
|
|
|
* @param string $level |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
378 |
|
private static function hasLogLevel($heading, $level) |
139
|
|
|
{ |
140
|
378 |
|
return Str::contains($heading, strtoupper(".{$level}:")); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: