5
5
use Encore \Admin \Extension ;
6
6
7
7
/**
8
- * Class LogViewer
9
- * @package Encore\Admin\LogViewer
8
+ * Class LogViewer.
10
9
*/
11
10
class LogViewer extends Extension
12
11
{
@@ -66,16 +65,16 @@ public function __construct($file = null)
66
65
/**
67
66
* Get file path by giving log file name.
68
67
*
69
- * @return string
70
- *
71
68
* @throws \Exception
69
+ *
70
+ * @return string
72
71
*/
73
72
public function getFilePath ()
74
73
{
75
74
if (!$ this ->filePath ) {
76
75
$ path = sprintf (storage_path ('logs/%s ' ), $ this ->file );
77
76
78
- if (! file_exists ($ path )) {
77
+ if (!file_exists ($ path )) {
79
78
throw new \Exception ('log not exists! ' );
80
79
}
81
80
@@ -105,7 +104,7 @@ public function getFilesize()
105
104
public function getLogFiles ($ count = 20 )
106
105
{
107
106
$ files = glob (storage_path ('logs/* ' ));
108
- $ files = array_combine ($ files , array_map (" filemtime " , $ files ));
107
+ $ files = array_combine ($ files , array_map (' filemtime ' , $ files ));
109
108
arsort ($ files );
110
109
111
110
$ files = array_map ('basename ' , array_keys ($ files ));
@@ -163,21 +162,24 @@ public function getNextPageUrl()
163
162
* @param int $seek
164
163
* @param int $lines
165
164
* @param int $buffer
165
+ *
166
166
* @return array
167
167
*
168
168
* @see http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/
169
169
*/
170
170
public function fetch ($ seek = 0 , $ lines = 20 , $ buffer = 4096 )
171
171
{
172
- $ f = fopen ($ this ->filePath , " rb " );
172
+ $ f = fopen ($ this ->filePath , ' rb ' );
173
173
174
174
if ($ seek ) {
175
175
fseek ($ f , abs ($ seek ));
176
176
} else {
177
177
fseek ($ f , 0 , SEEK_END );
178
178
}
179
179
180
- if (fread ($ f , 1 ) != "\n" ) $ lines -= 1 ;
180
+ if (fread ($ f , 1 ) != "\n" ) {
181
+ $ lines -= 1 ;
182
+ }
181
183
fseek ($ f , -1 , SEEK_CUR );
182
184
183
185
// 从前往后读,上一页
@@ -187,28 +189,27 @@ public function fetch($seek = 0, $lines = 20, $buffer = 4096)
187
189
188
190
$ this ->pageOffset ['start ' ] = ftell ($ f );
189
191
190
- while (!feof ($ f ) && $ lines >= 0 ) {
192
+ while (!feof ($ f ) && $ lines >= 0 ) {
191
193
$ output = $ output .($ chunk = fread ($ f , $ buffer ));
192
194
$ lines -= substr_count ($ chunk , "\n[20 " );
193
195
}
194
196
195
197
$ this ->pageOffset ['end ' ] = ftell ($ f );
196
198
197
- while ($ lines ++ < 0 ) {
198
- $ strpos = strrpos ($ output , "\n[20 " )+ 1 ;
199
+ while ($ lines ++ < 0 ) {
200
+ $ strpos = strrpos ($ output , "\n[20 " ) + 1 ;
199
201
$ _ = mb_strlen ($ output , '8bit ' ) - $ strpos ;
200
202
$ output = substr ($ output , 0 , $ strpos );
201
203
$ this ->pageOffset ['end ' ] -= $ _ ;
202
204
}
203
205
204
- // 从后往前读,下一页
206
+ // 从后往前读,下一页
205
207
} else {
206
-
207
208
$ output = '' ;
208
209
209
210
$ this ->pageOffset ['end ' ] = ftell ($ f );
210
211
211
- while (ftell ($ f ) > 0 && $ lines >= 0 ) {
212
+ while (ftell ($ f ) > 0 && $ lines >= 0 ) {
212
213
$ offset = min (ftell ($ f ), $ buffer );
213
214
fseek ($ f , -$ offset , SEEK_CUR );
214
215
$ output = ($ chunk = fread ($ f , $ offset )).$ output ;
@@ -218,7 +219,7 @@ public function fetch($seek = 0, $lines = 20, $buffer = 4096)
218
219
219
220
$ this ->pageOffset ['start ' ] = ftell ($ f );
220
221
221
- while ($ lines ++ < 0 ) {
222
+ while ($ lines ++ < 0 ) {
222
223
$ strpos = strpos ($ output , "\n[20 " ) + 1 ;
223
224
$ output = substr ($ output , $ strpos );
224
225
$ this ->pageOffset ['start ' ] += $ strpos ;
@@ -233,14 +234,14 @@ public function fetch($seek = 0, $lines = 20, $buffer = 4096)
233
234
/**
234
235
* Get tail logs in log file.
235
236
*
236
- * @param integer $seek
237
+ * @param int $seek
237
238
*
238
239
* @return array
239
240
*/
240
241
public function tail ($ seek )
241
242
{
242
243
// Open the file
243
- $ f = fopen ($ this ->filePath , " rb " );
244
+ $ f = fopen ($ this ->filePath , ' rb ' );
244
245
245
246
if (!$ seek ) {
246
247
// Jump to last character
@@ -272,11 +273,12 @@ public function tail($seek)
272
273
* Render table row.
273
274
*
274
275
* @param $log
276
+ *
275
277
* @return string
276
278
*/
277
279
protected function renderTableRow ($ log )
278
280
{
279
- $ color = LogViewer ::$ levelColors [$ log ['level ' ]] ?? 'black ' ;
281
+ $ color = self ::$ levelColors [$ log ['level ' ]] ?? 'black ' ;
280
282
281
283
$ index = uniqid ();
282
284
@@ -304,13 +306,13 @@ protected function renderTableRow($log)
304
306
</tr>
305
307
$ trace
306
308
TPL ;
307
-
308
309
}
309
310
310
311
/**
311
312
* Parse raw log text to array.
312
313
*
313
314
* @param $raw
315
+ *
314
316
* @return array
315
317
*/
316
318
protected function parseLog ($ raw )
0 commit comments