Skip to content

Commit 98a333e

Browse files
elminsonrap2hpoutre
authored andcommitted
Clear PR for Multiple Log View Locations #162 (#178)
1 parent 14a591f commit 98a333e

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ public function __construct()
5454
*/
5555
public function setFolder($folder)
5656
{
57-
$logsPath = $this->storage_path . '/' . $folder;
58-
59-
if (app('files')->exists($logsPath)) {
57+
if (app('files')->exists($folder)) {
6058
$this->folder = $folder;
6159
}
60+
if(is_array($this->storage_path)){
61+
foreach ($this->storage_path as $value) {
62+
$logsPath = $value . '/' . $folder;
63+
if (app('files')->exists($logsPath)) {
64+
$this->folder = $folder;
65+
break;
66+
}
67+
}
68+
}
6269
}
6370

6471
/**
@@ -81,20 +88,27 @@ public function setFile($file)
8188
*/
8289
public function pathToLogFile($file)
8390
{
84-
$logsPath = $this->storage_path;
85-
$logsPath .= ($this->folder) ? '/' . $this->folder : '';
8691

8792
if (app('files')->exists($file)) { // try the absolute path
8893
return $file;
8994
}
95+
if (is_array($this->storage_path)) {
96+
foreach ($this->storage_path as $folder) {
97+
if (app('files')->exists($folder . '/' . $file)) { // try the absolute path
98+
$file = $folder . '/' . $file;
99+
break;
100+
}
101+
}
102+
return $file;
103+
}
90104

105+
$logsPath = $this->storage_path;
106+
$logsPath .= ($this->folder) ? '/' . $this->folder : '';
91107
$file = $logsPath . '/' . $file;
92-
93108
// check if requested file is really in the logs directory
94109
if (dirname($file) !== $logsPath) {
95110
throw new \Exception('No such log file');
96111
}
97-
98112
return $file;
99113
}
100114

@@ -129,7 +143,9 @@ public function all()
129143
$this->file = $log_file[0];
130144
}
131145

132-
if (app('files')->size($this->file) > self::MAX_FILE_SIZE) return null;
146+
if (app('files')->size($this->file) > self::MAX_FILE_SIZE) {
147+
return null;
148+
}
133149

134150
$file = app('files')->get($this->file);
135151

@@ -150,12 +166,15 @@ public function all()
150166
foreach ($this->level->all() as $level) {
151167
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
152168

153-
preg_match($this->pattern->getPattern('current_log',0) . $level . $this->pattern->getPattern('current_log',1), $h[$i], $current);
154-
if (!isset($current[4])) continue;
169+
preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current);
170+
if (!isset($current[4])) {
171+
continue;
172+
}
155173

156174
$log[] = array(
157175
'context' => $current[3],
158176
'level' => $level,
177+
'folder' => $this->folder,
159178
'level_class' => $this->level->cssClass($level),
160179
'level_img' => $this->level->img($level),
161180
'date' => $current[1],
@@ -177,6 +196,7 @@ public function all()
177196
$log[] = [
178197
'context' => '',
179198
'level' => '',
199+
'folder' => '',
180200
'level_class' => '',
181201
'level_img' => '',
182202
'date' => $key + 1,
@@ -195,7 +215,16 @@ public function all()
195215
*/
196216
public function getFolders()
197217
{
198-
$folders = glob($this->storage_path.'/*', GLOB_ONLYDIR);
218+
$folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
219+
if (is_array($this->storage_path)) {
220+
foreach ($this->storage_path as $value) {
221+
$folders = array_merge(
222+
$folders,
223+
glob($value . '/*', GLOB_ONLYDIR)
224+
);
225+
}
226+
}
227+
199228
if (is_array($folders)) {
200229
foreach ($folders as $k => $folder) {
201230
$folders[$k] = basename($folder);
@@ -221,7 +250,22 @@ public function getFolderFiles($basename = false)
221250
public function getFiles($basename = false, $folder = '')
222251
{
223252
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
224-
$files = glob($this->storage_path.'/' . $folder . '/' . $pattern, preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0);
253+
$files = glob(
254+
$this->storage_path . '/' . $folder . '/' . $pattern,
255+
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
256+
);
257+
if (is_array($this->storage_path)) {
258+
foreach ($this->storage_path as $value) {
259+
$files = array_merge(
260+
$files,
261+
glob(
262+
$value . '/' . $folder . '/' . $pattern,
263+
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
264+
)
265+
);
266+
}
267+
}
268+
225269
$files = array_reverse($files);
226270
$files = array_filter($files, 'is_file');
227271
if ($basename && is_array($files)) {

src/views/log.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,15 @@ class="float-right expand btn btn-outline-dark btn-sm mb-2 ml-2"
157157
@endif
158158
<div class="p-3">
159159
@if($current_file)
160-
<a href="?dl={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
160+
<a href="?dl={{ \Illuminate\Support\Facades\Crypt::encrypt($current_folder ? $current_folder . "/" . $current_file : $current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
161161
<span class="fa fa-download"></span> Download file
162162
</a>
163163
-
164-
<a id="clean-log" href="?clean={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
164+
<a id="clean-log" href="?clean={{ \Illuminate\Support\Facades\Crypt::encrypt($current_folder ? $current_folder . "/" . $current_file : $current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
165165
<span class="fa fa-sync"></span> Clean file
166166
</a>
167167
-
168-
<a id="delete-log" href="?del={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
168+
<a id="delete-log" href="?del={{ \Illuminate\Support\Facades\Crypt::encrypt($current_folder ? $current_folder . "/" . $current_file : $current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
169169
<span class="fa fa-trash"></span> Delete file
170170
</a>
171171
@if(count($files) > 1)

0 commit comments

Comments
 (0)