-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathLogViewer.php
More file actions
239 lines (205 loc) · 6.35 KB
/
Copy pathLogViewer.php
File metadata and controls
239 lines (205 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace Backpack\LogManager\app\Classes;
use Illuminate\Support\Facades\Storage;
/**
* Class LogViewer.
*/
class LogViewer
{
/**
* @var string file
*/
private static $file;
/**
* Map debug levels to Bootstrap classes.
*
* @var array
*/
private static $levels_classes = [
'debug' => 'info',
'info' => 'info',
'notice' => 'info',
'warning' => 'warning',
'error' => 'danger',
'critical' => 'danger',
'alert' => 'danger',
'emergency' => 'danger',
'processed' => 'info',
];
/**
* Map debug levels to icon classes.
*
* @var array
*/
private static $levels_imgs = [
'debug' => 'info',
'info' => 'info',
'notice' => 'info',
'warning' => 'warning',
'error' => 'warning',
'critical' => 'warning',
'alert' => 'warning',
'emergency' => 'warning',
'processed' => 'info',
];
/**
* Log levels that are used.
*
* @var array
*/
private static $log_levels = [
'emergency',
'alert',
'critical',
'error',
'warning',
'notice',
'info',
'debug',
'processed',
];
/**
* Arbitrary max file size.
*/
const MAX_FILE_SIZE = 52428800;
/**
* @param string $file
*
* @throws \Exception
*/
public static function setFile($file)
{
$file = static::pathToLogFile($file);
if (app('files')->exists($file)) {
static::$file = $file;
}
}
/**
* @param string $file
*
* @throws
*
* @return string
*/
public static function pathToLogFile($file)
{
$logsPath = storage_path('logs');
if (app('files')->exists($file)) { // try the absolute path
return $file;
}
$file = $logsPath.'/'.$file;
// check if requested file is really in the logs directory
if (dirname($file) !== $logsPath) {
throw new \Exception('No such log file');
}
return $file;
}
/**
* @return string
*/
public static function getFileName()
{
return basename(static::$file);
}
/**
* @throws \Illuminate\Container\EntryNotFoundException
*
* @return array
*/
public static function all()
{
$log = [];
if (!static::$file) {
$log_file = static::getFiles();
if (!count($log_file)) {
return [];
}
static::$file = $log_file[0];
}
if (app('files')->size(static::$file) > static::MAX_FILE_SIZE) {
return [];
}
$file = app('files')->get(static::$file);
$pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/';
preg_match_all($pattern, $file, $headings);
if (!is_array($headings)) {
return $log;
}
$stack_trace = preg_split($pattern, $file);
if ($stack_trace[0] < 1) {
array_shift($stack_trace);
}
foreach ($headings as $h) {
for ($i = 0, $j = count($h); $i < $j; $i++) {
foreach (static::$log_levels as $level) {
if (strpos(strtolower($h[$i]), '.'.$level) || strpos(strtolower($h[$i]), $level.':')) {
$pattern = '/^\[(?P<date>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))\](?:.*?(?P<context>(\w+))\.|.*?)'.$level.': (?P<text>.*?)(?P<in_file> in .*?:[0-9]+)?$/i';
preg_match($pattern, $h[$i], $current);
if (!isset($current['text'])) {
continue;
}
$log[] = [
'context' => $current['context'],
'level' => $level,
'level_class' => static::$levels_classes[$level],
'level_img' => static::$levels_imgs[$level],
'date' => $current['date'],
'text' => $current['text'],
'in_file' => isset($current['in_file']) ? $current['in_file'] : null,
'stack' => preg_replace("/^\n*/", '', $stack_trace[$i]),
];
}
}
}
}
if (empty($log)) {
$lines = preg_split('/\r\n|\r|\n/', $file);
foreach ($lines as $line) {
if (!empty(trim($line))) {
$log[] = [
'context' => null,
'level' => 'info',
'level_class' => static::$levels_classes['info'],
'level_img' => static::$levels_imgs['info'],
'date' => null,
'text' => $line,
'in_file' => null,
'stack' => '',
];
}
}
}
return array_reverse($log);
}
/**
* @param bool $basename
* @param bool $sort_by_date Most recently modified files first
*
* @return array
*/
public static function getFiles($basename = false, $sort_by_date = false)
{
$files = glob(storage_path().'/logs/*.log');
$files = array_reverse($files);
$files = array_filter($files, 'is_file');
if ($basename && is_array($files)) {
foreach ($files as $k => $file) {
$disk = Storage::disk(config('backpack.base.root_disk_name'));
$file_name = basename($file);
if ($disk->exists('storage/logs/'.$file_name)) {
$files[$k] = [
'file_name' => $file_name,
'file_size' => $disk->size('storage/logs/'.$file_name),
'last_modified' => $disk->lastModified('storage/logs/'.$file_name),
];
}
}
if ($sort_by_date) {
usort($files, function ($a, $b) {
return $b['last_modified'] - $a['last_modified'];
});
}
}
return array_values($files);
}
}