Skip to content

Commit ce5b1d0

Browse files
authored
support for laravel daily logs (#209)
* resolve conflict * Add laravel 8 route format to readme * Add more tests for newly added methods * fix failing tests
1 parent 1a11c77 commit ce5b1d0

File tree

6 files changed

+286
-70
lines changed

6 files changed

+286
-70
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ install:
1010
- travis_retry composer install --no-interaction --prefer-source
1111

1212
script:
13-
- vendor/bin/phpunit
13+
- XDEBUG_MODE=coverage vendor/bin/phpunit
1414

1515
after_script:
1616
- wget https://scrutinizer-ci.com/ocular.phar

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
3232
Add a route in your web routes file:
3333
```php
3434
Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');
35+
36+
or
37+
// Laravel 8+
38+
Route::get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);
3539
```
3640

3741
Go to `http://myapp/logs` or some other route

src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php

Lines changed: 127 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,27 @@ public function __construct()
5555
public function setFolder($folder)
5656
{
5757
if (app('files')->exists($folder)) {
58+
5859
$this->folder = $folder;
5960
}
60-
if(is_array($this->storage_path)) {
61+
else if(is_array($this->storage_path)) {
62+
6163
foreach ($this->storage_path as $value) {
64+
6265
$logsPath = $value . '/' . $folder;
66+
6367
if (app('files')->exists($logsPath)) {
6468
$this->folder = $folder;
6569
break;
6670
}
6771
}
6872
} else {
69-
if ($this->storage_path) {
73+
7074
$logsPath = $this->storage_path . '/' . $folder;
7175
if (app('files')->exists($logsPath)) {
7276
$this->folder = $folder;
7377
}
74-
}
78+
7579
}
7680
}
7781

@@ -97,9 +101,11 @@ public function pathToLogFile($file)
97101
{
98102

99103
if (app('files')->exists($file)) { // try the absolute path
104+
100105
return $file;
101106
}
102107
if (is_array($this->storage_path)) {
108+
103109
foreach ($this->storage_path as $folder) {
104110
if (app('files')->exists($folder . '/' . $file)) { // try the absolute path
105111
$file = $folder . '/' . $file;
@@ -114,8 +120,9 @@ public function pathToLogFile($file)
114120
$file = $logsPath . '/' . $file;
115121
// check if requested file is really in the logs directory
116122
if (dirname($file) !== $logsPath) {
117-
throw new \Exception('No such log file');
123+
throw new \Exception('No such log file: '.$file);
118124
}
125+
119126
return $file;
120127
}
121128

@@ -228,29 +235,51 @@ public function all()
228235
return array_reverse($log);
229236
}
230237

231-
/**
232-
* @return array
233-
*/
234-
public function getFolders()
238+
/**Creates a multidimensional array
239+
* of subdirectories and files
240+
*
241+
* @param null $path
242+
*
243+
* @return array
244+
*/
245+
public function foldersAndFiles($path = null)
235246
{
236-
$folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
237-
if (is_array($this->storage_path)) {
238-
foreach ($this->storage_path as $value) {
239-
$folders = array_merge(
240-
$folders,
241-
glob($value . '/*', GLOB_ONLYDIR)
242-
);
243-
}
244-
}
247+
$contents = array();
248+
$dir = $path ? $path : $this->storage_path;
249+
foreach (scandir($dir) as $node) {
250+
if ($node == '.' || $node == '..') continue;
251+
$path = $dir . '\\' . $node;
252+
if (is_dir($path)) {
253+
$contents[$path] = $this->foldersAndFiles($path);
254+
} else {
255+
$contents[] = $path;
256+
}
257+
}
258+
259+
return $contents;
260+
}
245261

246-
if (is_array($folders)) {
247-
foreach ($folders as $k => $folder) {
248-
$folders[$k] = basename($folder);
249-
}
250-
}
251-
return array_values($folders);
262+
/**Returns an array of
263+
* all subdirectories of specified directory
264+
*
265+
* @param string $folder
266+
*
267+
* @return array
268+
*/
269+
public function getFolders($folder = '')
270+
{
271+
$folders = [];
272+
$listObject = new \RecursiveIteratorIterator(
273+
new \RecursiveDirectoryIterator($this->storage_path.'/'.$folder, \RecursiveDirectoryIterator::SKIP_DOTS),
274+
\RecursiveIteratorIterator::CHILD_FIRST
275+
);
276+
foreach ($listObject as $fileinfo) {
277+
if($fileinfo->isDir()) $folders[] = $fileinfo->getRealPath();
278+
}
279+
return $folders;
252280
}
253281

282+
254283
/**
255284
* @param bool $basename
256285
* @return array
@@ -267,30 +296,81 @@ public function getFolderFiles($basename = false)
267296
*/
268297
public function getFiles($basename = false, $folder = '')
269298
{
270-
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
271-
$files = glob(
272-
$this->storage_path . '/' . $folder . '/' . $pattern,
273-
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
274-
);
275-
if (is_array($this->storage_path)) {
276-
foreach ($this->storage_path as $value) {
277-
$files = array_merge(
278-
$files,
279-
glob(
280-
$value . '/' . $folder . '/' . $pattern,
281-
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
282-
)
283-
);
284-
}
285-
}
299+
$files = [];
300+
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
301+
$fullPath = $this->storage_path.'/'.$folder;
286302

287-
$files = array_reverse($files);
288-
$files = array_filter($files, 'is_file');
289-
if ($basename && is_array($files)) {
290-
foreach ($files as $k => $file) {
291-
$files[$k] = basename($file);
292-
}
293-
}
294-
return array_values($files);
303+
$listObject = new \RecursiveIteratorIterator(
304+
new \RecursiveDirectoryIterator($fullPath, \RecursiveDirectoryIterator::SKIP_DOTS),
305+
\RecursiveIteratorIterator::CHILD_FIRST
306+
);
307+
308+
foreach ($listObject as $fileinfo) {
309+
if(!$fileinfo->isDir() && strtolower(pathinfo($fileinfo->getRealPath(), PATHINFO_EXTENSION)) == explode('.', $pattern)[1])
310+
$files[] = $basename ? basename($fileinfo->getRealPath()) : $fileinfo->getRealPath();
311+
}
312+
return $files;
313+
314+
}
315+
316+
/**
317+
* @return string
318+
*/
319+
public function getStoragePath()
320+
{
321+
return $this->storage_path;
295322
}
323+
324+
/**
325+
* @param $path
326+
*
327+
* @return void
328+
*/
329+
public function setStoragePath($path)
330+
{
331+
$this->storage_path = $path;
332+
}
333+
334+
public static function directoryTreeStructure($storage_path, array $array)
335+
{
336+
foreach ($array as $k => $v) {
337+
if(is_dir( $k )) {
338+
339+
$exploded = explode( "\\", $k );
340+
$show = last( $exploded );
341+
342+
echo '<div class="list-group folder">
343+
<a href="?f='. \Illuminate\Support\Facades\Crypt::encrypt($k).'">
344+
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span
345+
class="fa fa-folder"></span> '.$show.'
346+
</a>
347+
</div>';
348+
349+
if ( is_array( $v ) ) {
350+
self::directoryTreeStructure( $storage_path, $v );
351+
}
352+
353+
}
354+
else {
355+
356+
$exploded = explode( "\\", $v );
357+
$show2 = last( $exploded );
358+
$folder = str_replace( $storage_path, "", rtrim( str_replace( $show2, "", $v ), "\\" ) );
359+
$file = $v;
360+
361+
362+
echo '<div class="list-group">
363+
<a href="?l='.\Illuminate\Support\Facades\Crypt::encrypt($file).'&f='.\Illuminate\Support\Facades\Crypt::encrypt($folder).'">
364+
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span
365+
class="fa fa-file"></span> '.$show2.'
366+
</a>
367+
</div>';
368+
369+
}
370+
}
371+
372+
return;
373+
}
374+
375+
296376
}

src/controllers/LogViewerController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function index()
6767
'files' => $this->log_viewer->getFiles(true),
6868
'current_file' => $this->log_viewer->getFileName(),
6969
'standardFormat' => true,
70+
'structure' => $this->log_viewer->foldersAndFiles(),
71+
'storage_path' => $this->log_viewer->getStoragePath(),
72+
7073
];
7174

7275
if ($this->request->wantsJson()) {

src/views/log.blade.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
.nowrap {
6767
white-space: nowrap;
6868
}
69+
.list-group {
70+
padding: 5px;
71+
}
72+
6973
7074
7175
@@ -180,19 +184,10 @@ function resetTheme() {
180184
<div class="list-group div-scroll">
181185
@foreach($folders as $folder)
182186
<div class="list-group-item">
183-
<a href="?f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}">
184-
<span class="fa fa-folder"></span> {{$folder}}
185-
</a>
186-
@if ($current_folder == $folder)
187-
<div class="list-group folder">
188-
@foreach($folder_files as $file)
189-
<a href="?l={{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}&f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}"
190-
class="list-group-item @if ($current_file == $file) llv-active @endif">
191-
{{$file}}
192-
</a>
193-
@endforeach
194-
</div>
195-
@endif
187+
<?php
188+
\Rap2hpoutre\LaravelLogViewer\LaravelLogViewer::DirectoryTreeStructure( $storage_path, $structure );
189+
?>
190+
196191
</div>
197192
@endforeach
198193
@foreach($files as $file)

0 commit comments

Comments
 (0)