Skip to content

Commit 5c8a4b1

Browse files
ssklrap2hpoutre
authored andcommitted
be able display subfolders of log folder. (#155)
1 parent 11d4d71 commit 5c8a4b1

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class LaravelLogViewer
1414
*/
1515
private static $file;
1616

17+
/**
18+
* @var string folder
19+
*/
20+
private static $folder;
21+
1722
private static $levels_classes = [
1823
'debug' => 'info',
1924
'info' => 'info',
@@ -59,6 +64,18 @@ class LaravelLogViewer
5964

6065
const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry
6166

67+
/**
68+
* @param string $folder
69+
*/
70+
public static function setFolder($folder)
71+
{
72+
$logsPath = storage_path('logs') . '/' . $folder;
73+
74+
if (app('files')->exists($logsPath)) {
75+
self::$folder = $folder;
76+
}
77+
}
78+
6279
/**
6380
* @param string $file
6481
*/
@@ -79,7 +96,8 @@ public static function setFile($file)
7996
public static function pathToLogFile($file)
8097
{
8198
$logsPath = storage_path('logs');
82-
99+
$logsPath .= (self::$folder) ? '/' . self::$folder : '' ;
100+
83101
if (app('files')->exists($file)) { // try the absolute path
84102
return $file;
85103
}
@@ -94,6 +112,14 @@ public static function pathToLogFile($file)
94112
return $file;
95113
}
96114

115+
/**
116+
* @return string
117+
*/
118+
public static function getFolderName()
119+
{
120+
return self::$folder;
121+
}
122+
97123
/**
98124
* @return string
99125
*/
@@ -112,7 +138,7 @@ public static function all()
112138
$pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';
113139

114140
if (!self::$file) {
115-
$log_file = self::getFiles();
141+
$log_file = (!self::$folder) ? self::getFiles() : self::getFolderFiles();
116142
if(!count($log_file)) {
117143
return [];
118144
}
@@ -180,14 +206,37 @@ public static function all()
180206
return array_reverse($log);
181207
}
182208

209+
/**
210+
* @return array
211+
*/
212+
public static function getFolders()
213+
{
214+
$folders = glob(storage_path() . '/logs/*', GLOB_ONLYDIR);
215+
if (is_array($folders)) {
216+
foreach ($folders as $k => $folder) {
217+
$folders[$k] = basename($folder);
218+
}
219+
}
220+
return array_values($folders);
221+
}
222+
223+
/**
224+
* @param bool $basename
225+
* @return array
226+
*/
227+
public static function getFolderFiles($basename = false)
228+
{
229+
return self::getFiles($basename, self::$folder);
230+
}
231+
183232
/**
184233
* @param bool $basename
185234
* @return array
186235
*/
187-
public static function getFiles($basename = false)
236+
public static function getFiles($basename = false, $folder = '')
188237
{
189238
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
190-
$files = glob(storage_path() . '/logs/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
239+
$files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
191240
$files = array_reverse($files);
192241
$files = array_filter($files, 'is_file');
193242
if ($basename && is_array($files)) {

src/controllers/LogViewerController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@ public function __construct ()
3030
*/
3131
public function index()
3232
{
33+
$folderFiles = [];
34+
if ($this->request->input('f')) {
35+
LaravelLogViewer::setFolder(Crypt::decrypt($this->request->input('f')));
36+
$folderFiles = LaravelLogViewer::getFolderFiles(true);
37+
}
3338
if ($this->request->input('l')) {
3439
LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l')));
3540
}
3641

3742
if ($early_return = $this->earlyReturn()) {
3843
return $early_return;
3944
}
40-
45+
4146
$data = [
4247
'logs' => LaravelLogViewer::all(),
48+
'folders' => LaravelLogViewer::getFolders(),
49+
'current_folder' => LaravelLogViewer::getFolderName(),
50+
'folder_files' => $folderFiles,
4351
'files' => LaravelLogViewer::getFiles(true),
4452
'current_file' => LaravelLogViewer::getFileName(),
4553
'standardFormat' => true,

src/views/log.blade.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
.list-group-item {
5454
word-wrap: break-word;
5555
}
56+
57+
.folder {
58+
padding-top: 5px;
59+
}
5660
</style>
5761
</head>
5862
<body>
@@ -62,6 +66,23 @@
6266
<h1><i class="fa fa-calendar" aria-hidden="true"></i> Laravel Log Viewer</h1>
6367
<p class="text-muted"><i>by Rap2h</i></p>
6468
<div class="list-group">
69+
@foreach($folders as $folder)
70+
<div class="list-group-item">
71+
<a href="?f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}">
72+
<span class="fa fa-folder"></span> {{$folder}}
73+
</a>
74+
@if ($current_folder == $folder)
75+
<div class="list-group folder">
76+
@foreach($folder_files as $file)
77+
<a href="?l={{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}&f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}"
78+
class="list-group-item @if ($current_file == $file) llv-active @endif">
79+
{{$file}}
80+
</a>
81+
@endforeach
82+
</div>
83+
@endif
84+
</div>
85+
@endforeach
6586
@foreach($files as $file)
6687
<a href="?l={{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}"
6788
class="list-group-item @if ($current_file == $file) llv-active @endif">

0 commit comments

Comments
 (0)