1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rap2hpoutre\LaravelLogViewer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LaravelLogViewer |
7
|
|
|
* @package Rap2hpoutre\LaravelLogViewer |
8
|
|
|
*/ |
9
|
|
|
class LaravelLogViewer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string file |
13
|
|
|
*/ |
14
|
|
|
private $file; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string folder |
18
|
|
|
*/ |
19
|
|
|
private $folder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string storage_path |
23
|
|
|
*/ |
24
|
|
|
private $storage_path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Why? Uh... Sorry |
28
|
|
|
*/ |
29
|
|
|
const MAX_FILE_SIZE = 52428800; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Level level |
33
|
|
|
*/ |
34
|
|
|
private $level; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Pattern pattern |
38
|
|
|
*/ |
39
|
|
|
private $pattern; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* LaravelLogViewer constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$this->level = new Level(); |
47
|
|
|
$this->pattern = new Pattern(); |
48
|
|
|
$this->storage_path = function_exists('config') ? config('logviewer.storage_path', storage_path('logs')) : storage_path('logs'); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $folder |
54
|
|
|
*/ |
55
|
|
|
public function setFolder($folder) |
56
|
|
|
{ |
57
|
|
|
$logsPath = $this->storage_path . '/' . $folder; |
58
|
|
|
|
59
|
|
|
if (app('files')->exists($logsPath)) { |
60
|
|
|
$this->folder = $folder; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $file |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public function setFile($file) |
69
|
|
|
{ |
70
|
|
|
$file = $this->pathToLogFile($file); |
71
|
|
|
|
72
|
|
|
if (app('files')->exists($file)) { |
73
|
|
|
$this->file = $file; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $file |
79
|
|
|
* @return string |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
public function pathToLogFile($file) |
83
|
|
|
{ |
84
|
|
|
if (app('files')->exists($file)) { // try the absolute path |
85
|
|
|
return $file; |
86
|
|
|
} |
87
|
|
|
if (is_array($this->storage_path)) { |
88
|
|
|
foreach ($this->storage_path as $value) { |
89
|
|
|
if (app('files')->exists($value . '/' . $file)) { // try the absolute path |
90
|
|
|
$file = $value . '/' . $file; |
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
if (!is_array($this->storage_path)) { |
96
|
|
|
$logsPath = $this->storage_path; |
97
|
|
|
$logsPath .= ($this->folder) ? '/' . $this->folder : ''; |
98
|
|
|
$file = $logsPath . '/' . $file; |
99
|
|
|
// check if requested file is really in the logs directory |
100
|
|
|
if (dirname($file) !== $logsPath) { |
101
|
|
|
throw new \Exception('No such log file'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $file; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getFolderName() |
111
|
|
|
{ |
112
|
|
|
return $this->folder; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getFileName() |
119
|
|
|
{ |
120
|
|
|
return basename($this->file); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function all() |
127
|
|
|
{ |
128
|
|
|
$log = array(); |
129
|
|
|
|
130
|
|
|
if (!$this->file) { |
131
|
|
|
$log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles(); |
132
|
|
|
if (!count($log_file)) { |
133
|
|
|
return []; |
134
|
|
|
} |
135
|
|
|
$this->file = $log_file[0]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (app('files')->size($this->file) > self::MAX_FILE_SIZE) { |
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$file = app('files')->get($this->file); |
143
|
|
|
|
144
|
|
|
preg_match_all($this->pattern->getPattern('logs'), $file, $headings); |
145
|
|
|
|
146
|
|
|
if (!is_array($headings)) { |
147
|
|
|
return $log; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$log_data = preg_split($this->pattern->getPattern('logs'), $file); |
151
|
|
|
|
152
|
|
|
if ($log_data[0] < 1) { |
153
|
|
|
array_shift($log_data); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
foreach ($headings as $h) { |
157
|
|
|
for ($i = 0, $j = count($h); $i < $j; $i++) { |
158
|
|
|
foreach ($this->level->all() as $level) { |
159
|
|
|
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) { |
160
|
|
|
|
161
|
|
|
preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current); |
162
|
|
|
if (!isset($current[4])) { |
163
|
|
|
continue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$log[] = array( |
167
|
|
|
'context' => $current[3], |
168
|
|
|
'level' => $level, |
169
|
|
|
'level_class' => $this->level->cssClass($level), |
170
|
|
|
'level_img' => $this->level->img($level), |
171
|
|
|
'date' => $current[1], |
172
|
|
|
'text' => $current[4], |
173
|
|
|
'in_file' => isset($current[5]) ? $current[5] : null, |
174
|
|
|
'stack' => preg_replace("/^\n*/", '', $log_data[$i]) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (empty($log)) { |
182
|
|
|
|
183
|
|
|
$lines = explode(PHP_EOL, $file); |
184
|
|
|
$log = []; |
185
|
|
|
|
186
|
|
|
foreach ($lines as $key => $line) { |
187
|
|
|
$log[] = [ |
188
|
|
|
'context' => '', |
189
|
|
|
'level' => '', |
190
|
|
|
'level_class' => '', |
191
|
|
|
'level_img' => '', |
192
|
|
|
'date' => $key + 1, |
193
|
|
|
'text' => $line, |
194
|
|
|
'in_file' => null, |
195
|
|
|
'stack' => '', |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return array_reverse($log); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function getFolders() |
207
|
|
|
{ |
208
|
|
|
$folders = []; |
209
|
|
|
if (is_array($this->storage_path)) { |
210
|
|
|
foreach ($this->storage_path as $value) { |
211
|
|
|
$folders = array_merge( |
212
|
|
|
$folders, |
213
|
|
|
glob($value . '/*', GLOB_ONLYDIR) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if (!is_array($this->storage_path)) { |
219
|
|
|
$folders = glob($this->storage_path . '/*', GLOB_ONLYDIR); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (is_array($folders)) { |
223
|
|
|
foreach ($folders as $k => $folder) { |
224
|
|
|
$folders[$k] = basename($folder); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
return array_values($folders); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param bool $basename |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function getFolderFiles($basename = false) |
235
|
|
|
{ |
236
|
|
|
return $this->getFiles($basename, $this->folder); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param bool $basename |
241
|
|
|
* @param string $folder |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
public function getFiles($basename = false, $folder = '') |
245
|
|
|
{ |
246
|
|
|
$files = []; |
247
|
|
|
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log'; |
248
|
|
|
if (is_array($this->storage_path)) { |
249
|
|
|
foreach ($this->storage_path as $value) { |
250
|
|
|
$files = array_merge( |
251
|
|
|
$files, |
252
|
|
|
glob( |
253
|
|
|
$value . '/' . $folder . '/' . $pattern, |
254
|
|
|
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0 |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if (!is_array($this->storage_path)) { |
261
|
|
|
$files = glob( |
262
|
|
|
$this->storage_path . '/' . $folder . '/' . $pattern, |
263
|
|
|
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0 |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$files = array_reverse($files); |
268
|
|
|
$files = array_filter($files, 'is_file'); |
269
|
|
|
if ($basename && is_array($files)) { |
270
|
|
|
foreach ($files as $k => $file) { |
271
|
|
|
$files[$k] = basename($file); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
return array_values($files); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|