|
1
|
|
|
<?php |
|
2
|
|
|
namespace Rap2hpoutre\LaravelLogViewer; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LogLevel; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class LaravelLogViewer |
|
8
|
|
|
* @package Rap2hpoutre\LaravelLogViewer |
|
9
|
|
|
*/ |
|
10
|
|
|
class LaravelLogViewer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string file |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $file; |
|
16
|
|
|
|
|
17
|
|
|
private static $levels_classes = [ |
|
18
|
|
|
'debug' => 'info', |
|
19
|
|
|
'info' => 'info', |
|
20
|
|
|
'notice' => 'info', |
|
21
|
|
|
'warning' => 'warning', |
|
22
|
|
|
'error' => 'danger', |
|
23
|
|
|
'critical' => 'danger', |
|
24
|
|
|
'alert' => 'danger', |
|
25
|
|
|
'emergency' => 'danger', |
|
26
|
|
|
'processed' => 'info', |
|
27
|
|
|
'failed' => 'warning', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
private static $levels_imgs = [ |
|
31
|
|
|
'debug' => 'info-circle', |
|
32
|
|
|
'info' => 'info-circle', |
|
33
|
|
|
'notice' => 'info-circle', |
|
34
|
|
|
'warning' => 'exclamation-triangle', |
|
35
|
|
|
'error' => 'exclamation-triangle', |
|
36
|
|
|
'critical' => 'exclamation-triangle', |
|
37
|
|
|
'alert' => 'exclamation-triangle', |
|
38
|
|
|
'emergency' => 'exclamation-triangle', |
|
39
|
|
|
'processed' => 'info-circle', |
|
40
|
|
|
'failed' => 'exclamation-triangle' |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Log levels that are used |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private static $log_levels = [ |
|
48
|
|
|
'emergency', |
|
49
|
|
|
'alert', |
|
50
|
|
|
'critical', |
|
51
|
|
|
'error', |
|
52
|
|
|
'warning', |
|
53
|
|
|
'notice', |
|
54
|
|
|
'info', |
|
55
|
|
|
'debug', |
|
56
|
|
|
'processed', |
|
57
|
|
|
'failed' |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $file |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function setFile($file) |
|
66
|
|
|
{ |
|
67
|
|
|
$file = self::pathToLogFile($file); |
|
68
|
|
|
|
|
69
|
|
|
if (app('files')->exists($file)) { |
|
70
|
|
|
self::$file = $file; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $file |
|
76
|
|
|
* @return string |
|
77
|
|
|
* @throws \Exception |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function pathToLogFile($file) |
|
80
|
|
|
{ |
|
81
|
|
|
$logsPath = storage_path('logs'); |
|
82
|
|
|
|
|
83
|
|
|
if (app('files')->exists($file)) { // try the absolute path |
|
84
|
|
|
return $file; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$file = $logsPath . '/' . $file; |
|
88
|
|
|
|
|
89
|
|
|
// check if requested file is really in the logs directory |
|
90
|
|
|
if (dirname($file) !== $logsPath) { |
|
91
|
|
|
throw new \Exception('No such log file'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $file; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function getFileName() |
|
101
|
|
|
{ |
|
102
|
|
|
return basename(self::$file); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function all() |
|
109
|
|
|
{ |
|
110
|
|
|
if (!self::$file) { |
|
111
|
|
|
$log_file = self::getFiles(); |
|
112
|
|
|
if(!count($log_file)) { |
|
113
|
|
|
return []; |
|
114
|
|
|
} |
|
115
|
|
|
self::$file = $log_file[0]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (app('files')->size(self::$file) > self::MAX_FILE_SIZE) return null; |
|
119
|
|
|
|
|
120
|
|
|
$file = app('files')->get(self::$file); |
|
121
|
|
|
|
|
122
|
|
|
$log = static::tryJson($file); |
|
|
|
|
|
|
123
|
|
|
if(!empty($log)) return $log; |
|
124
|
|
|
$log = static::tryLine($file); |
|
|
|
|
|
|
125
|
|
|
if(!empty($log)) return $log; |
|
126
|
|
|
return static::tryDefault($file); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private static function tryLine(&$file) |
|
130
|
|
|
{ |
|
131
|
|
|
$log = []; |
|
132
|
|
|
$pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/'; |
|
133
|
|
|
|
|
134
|
|
|
preg_match_all($pattern, $file, $headings); |
|
135
|
|
|
|
|
136
|
|
|
if (!is_array($headings)) { |
|
137
|
|
|
return $log; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$log_data = preg_split($pattern, $file); |
|
141
|
|
|
|
|
142
|
|
|
if ($log_data[0] < 1) { |
|
143
|
|
|
array_shift($log_data); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
foreach ($headings as $h) { |
|
147
|
|
|
for ($i=0, $j = count($h); $i < $j; $i++) { |
|
148
|
|
|
foreach (self::$log_levels as $level) { |
|
149
|
|
|
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) { |
|
150
|
|
|
|
|
151
|
|
|
preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current); |
|
152
|
|
|
if (!isset($current[4])) continue; |
|
153
|
|
|
|
|
154
|
|
|
$log[] = array( |
|
155
|
|
|
'channel' => $current[3], |
|
156
|
|
|
'level' => $level, |
|
157
|
|
|
'level_class' => self::$levels_classes[$level], |
|
158
|
|
|
'level_img' => self::$levels_imgs[$level], |
|
159
|
|
|
'date' => $current[1], |
|
160
|
|
|
'text' => $current[4], |
|
161
|
|
|
'in_file' => isset($current[5]) ? $current[5] : null, |
|
162
|
|
|
'stack' => preg_replace("/^\n*/", '', $log_data[$i]), |
|
163
|
|
|
'extra' => null |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return array_reverse($log); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private static function tryJson(&$file) |
|
174
|
|
|
{ |
|
175
|
|
|
$log = []; |
|
176
|
|
|
$lines = explode("\n", trim($file)); |
|
177
|
|
|
|
|
178
|
|
|
foreach($lines as $line) { |
|
179
|
|
|
$decoded = json_decode($line, true); |
|
180
|
|
|
if($decoded === null) { |
|
181
|
|
|
return []; |
|
182
|
|
|
} |
|
183
|
|
|
$level = strtolower($decoded['level_name']); |
|
184
|
|
|
$stack = null; |
|
185
|
|
|
$extra = array_merge($decoded['context'] ?? [], $decoded['extra'] ?? []); |
|
186
|
|
|
if(isset($decoded['context']) && isset($decoded['context']['exception'])) { |
|
187
|
|
|
$stack = $decoded['context']['exception']['trace']; |
|
188
|
|
|
unset($extra['exception']); |
|
189
|
|
|
} |
|
190
|
|
|
$log[] = [ |
|
191
|
|
|
'channel' => $decoded['channel'], |
|
192
|
|
|
'level' => $level, |
|
193
|
|
|
'level_class' => self::$levels_classes[$level], |
|
194
|
|
|
'level_img' => self::$levels_imgs[$level], |
|
195
|
|
|
'date' => $decoded['datetime']['date'], |
|
196
|
|
|
'text' => $decoded['message'], |
|
197
|
|
|
'in_file' => null, |
|
198
|
|
|
'stack' => $stack, |
|
199
|
|
|
'extra' => json_encode($extra, JSON_PRETTY_PRINT) |
|
200
|
|
|
]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return array_reverse($log); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
private static function tryDefault(&$file) |
|
207
|
|
|
{ |
|
208
|
|
|
$lines = explode(PHP_EOL, $file); |
|
209
|
|
|
$log = []; |
|
210
|
|
|
|
|
211
|
|
|
foreach($lines as $key => $line) { |
|
212
|
|
|
$log[] = [ |
|
213
|
|
|
'channel' => '', |
|
214
|
|
|
'level' => '', |
|
215
|
|
|
'level_class' => '', |
|
216
|
|
|
'level_img' => '', |
|
217
|
|
|
'date' => $key+1, |
|
218
|
|
|
'text' => $line, |
|
219
|
|
|
'in_file' => null, |
|
220
|
|
|
'stack' => '', |
|
221
|
|
|
'extra' => null |
|
222
|
|
|
]; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return array_reverse($log); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param bool $basename |
|
230
|
|
|
* @return array |
|
231
|
|
|
*/ |
|
232
|
|
|
public static function getFiles($basename = false) |
|
233
|
|
|
{ |
|
234
|
|
|
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log'; |
|
235
|
|
|
$files = glob(storage_path() . '/logs/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0); |
|
236
|
|
|
$files = array_reverse($files); |
|
237
|
|
|
$files = array_filter($files, 'is_file'); |
|
238
|
|
|
if ($basename && is_array($files)) { |
|
239
|
|
|
foreach ($files as $k => $file) { |
|
240
|
|
|
$files[$k] = basename($file); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
return array_values($files); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
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
SomeClassto useselfinstead: