Skip to content

Commit

Permalink
Clear PR for Multiple Log View Locations #162 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
elminson authored and rap2hpoutre committed Feb 27, 2019
1 parent 14a591f commit 98a333e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
68 changes: 56 additions & 12 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ public function __construct()
*/
public function setFolder($folder)
{
$logsPath = $this->storage_path . '/' . $folder;

if (app('files')->exists($logsPath)) {
if (app('files')->exists($folder)) {
$this->folder = $folder;
}
if(is_array($this->storage_path)){
foreach ($this->storage_path as $value) {
$logsPath = $value . '/' . $folder;
if (app('files')->exists($logsPath)) {
$this->folder = $folder;
break;
}
}
}
}

/**
Expand All @@ -81,20 +88,27 @@ public function setFile($file)
*/
public function pathToLogFile($file)
{
$logsPath = $this->storage_path;
$logsPath .= ($this->folder) ? '/' . $this->folder : '';

if (app('files')->exists($file)) { // try the absolute path
return $file;
}
if (is_array($this->storage_path)) {
foreach ($this->storage_path as $folder) {
if (app('files')->exists($folder . '/' . $file)) { // try the absolute path
$file = $folder . '/' . $file;
break;
}
}
return $file;
}

$logsPath = $this->storage_path;
$logsPath .= ($this->folder) ? '/' . $this->folder : '';
$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;
}

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

if (app('files')->size($this->file) > self::MAX_FILE_SIZE) return null;
if (app('files')->size($this->file) > self::MAX_FILE_SIZE) {
return null;
}

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

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

preg_match($this->pattern->getPattern('current_log',0) . $level . $this->pattern->getPattern('current_log',1), $h[$i], $current);
if (!isset($current[4])) continue;
preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current);
if (!isset($current[4])) {
continue;
}

$log[] = array(
'context' => $current[3],
'level' => $level,
'folder' => $this->folder,
'level_class' => $this->level->cssClass($level),
'level_img' => $this->level->img($level),
'date' => $current[1],
Expand All @@ -177,6 +196,7 @@ public function all()
$log[] = [
'context' => '',
'level' => '',
'folder' => '',
'level_class' => '',
'level_img' => '',
'date' => $key + 1,
Expand All @@ -195,7 +215,16 @@ public function all()
*/
public function getFolders()
{
$folders = glob($this->storage_path.'/*', GLOB_ONLYDIR);
$folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
if (is_array($this->storage_path)) {
foreach ($this->storage_path as $value) {
$folders = array_merge(
$folders,
glob($value . '/*', GLOB_ONLYDIR)
);
}
}

if (is_array($folders)) {
foreach ($folders as $k => $folder) {
$folders[$k] = basename($folder);
Expand All @@ -221,7 +250,22 @@ public function getFolderFiles($basename = false)
public function getFiles($basename = false, $folder = '')
{
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
$files = glob($this->storage_path.'/' . $folder . '/' . $pattern, preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0);
$files = glob(
$this->storage_path . '/' . $folder . '/' . $pattern,
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
);
if (is_array($this->storage_path)) {
foreach ($this->storage_path as $value) {
$files = array_merge(
$files,
glob(
$value . '/' . $folder . '/' . $pattern,
preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
)
);
}
}

$files = array_reverse($files);
$files = array_filter($files, 'is_file');
if ($basename && is_array($files)) {
Expand Down
6 changes: 3 additions & 3 deletions src/views/log.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ class="float-right expand btn btn-outline-dark btn-sm mb-2 ml-2"
@endif
<div class="p-3">
@if($current_file)
<a href="?dl={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
<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) : '' }}">
<span class="fa fa-download"></span> Download file
</a>
-
<a id="clean-log" href="?clean={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
<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) : '' }}">
<span class="fa fa-sync"></span> Clean file
</a>
-
<a id="delete-log" href="?del={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}{{ ($current_folder) ? '&f=' . \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : '' }}">
<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) : '' }}">
<span class="fa fa-trash"></span> Delete file
</a>
@if(count($files) > 1)
Expand Down

0 comments on commit 98a333e

Please sign in to comment.