Skip to content

Commit

Permalink
support for laravel daily logs (#209)
Browse files Browse the repository at this point in the history
* resolve conflict

* Add laravel 8 route format to readme

* Add more tests for newly added methods

* fix failing tests
  • Loading branch information
djunehor committed Nov 4, 2021
1 parent 1a11c77 commit ce5b1d0
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- travis_retry composer install --no-interaction --prefer-source

script:
- vendor/bin/phpunit
- XDEBUG_MODE=coverage vendor/bin/phpunit

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
Add a route in your web routes file:
```php
Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');

or
// Laravel 8+
Route::get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);
```

Go to `http://myapp/logs` or some other route
Expand Down
174 changes: 127 additions & 47 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ public function __construct()
public function setFolder($folder)
{
if (app('files')->exists($folder)) {

$this->folder = $folder;
}
if(is_array($this->storage_path)) {
else if(is_array($this->storage_path)) {

foreach ($this->storage_path as $value) {

$logsPath = $value . '/' . $folder;

if (app('files')->exists($logsPath)) {
$this->folder = $folder;
break;
}
}
} else {
if ($this->storage_path) {

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

}
}

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

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;
Expand All @@ -114,8 +120,9 @@ public function pathToLogFile($file)
$file = $logsPath . '/' . $file;
// check if requested file is really in the logs directory
if (dirname($file) !== $logsPath) {
throw new \Exception('No such log file');
throw new \Exception('No such log file: '.$file);
}

return $file;
}

Expand Down Expand Up @@ -228,29 +235,51 @@ public function all()
return array_reverse($log);
}

/**
* @return array
*/
public function getFolders()
/**Creates a multidimensional array
* of subdirectories and files
*
* @param null $path
*
* @return array
*/
public function foldersAndFiles($path = null)
{
$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)
);
}
}
$contents = array();
$dir = $path ? $path : $this->storage_path;
foreach (scandir($dir) as $node) {
if ($node == '.' || $node == '..') continue;
$path = $dir . '\\' . $node;
if (is_dir($path)) {
$contents[$path] = $this->foldersAndFiles($path);
} else {
$contents[] = $path;
}
}

return $contents;
}

if (is_array($folders)) {
foreach ($folders as $k => $folder) {
$folders[$k] = basename($folder);
}
}
return array_values($folders);
/**Returns an array of
* all subdirectories of specified directory
*
* @param string $folder
*
* @return array
*/
public function getFolders($folder = '')
{
$folders = [];
$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->storage_path.'/'.$folder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($listObject as $fileinfo) {
if($fileinfo->isDir()) $folders[] = $fileinfo->getRealPath();
}
return $folders;
}


/**
* @param bool $basename
* @return array
Expand All @@ -267,30 +296,81 @@ 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
);
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 = [];
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
$fullPath = $this->storage_path.'/'.$folder;

$files = array_reverse($files);
$files = array_filter($files, 'is_file');
if ($basename && is_array($files)) {
foreach ($files as $k => $file) {
$files[$k] = basename($file);
}
}
return array_values($files);
$listObject = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($fullPath, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($listObject as $fileinfo) {
if(!$fileinfo->isDir() && strtolower(pathinfo($fileinfo->getRealPath(), PATHINFO_EXTENSION)) == explode('.', $pattern)[1])
$files[] = $basename ? basename($fileinfo->getRealPath()) : $fileinfo->getRealPath();
}
return $files;

}

/**
* @return string
*/
public function getStoragePath()
{
return $this->storage_path;
}

/**
* @param $path
*
* @return void
*/
public function setStoragePath($path)
{
$this->storage_path = $path;
}

public static function directoryTreeStructure($storage_path, array $array)
{
foreach ($array as $k => $v) {
if(is_dir( $k )) {

$exploded = explode( "\\", $k );
$show = last( $exploded );

echo '<div class="list-group folder">
<a href="?f='. \Illuminate\Support\Facades\Crypt::encrypt($k).'">
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span
class="fa fa-folder"></span> '.$show.'
</a>
</div>';

if ( is_array( $v ) ) {
self::directoryTreeStructure( $storage_path, $v );
}

}
else {

$exploded = explode( "\\", $v );
$show2 = last( $exploded );
$folder = str_replace( $storage_path, "", rtrim( str_replace( $show2, "", $v ), "\\" ) );
$file = $v;


echo '<div class="list-group">
<a href="?l='.\Illuminate\Support\Facades\Crypt::encrypt($file).'&f='.\Illuminate\Support\Facades\Crypt::encrypt($folder).'">
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span
class="fa fa-file"></span> '.$show2.'
</a>
</div>';

}
}

return;
}


}
3 changes: 3 additions & 0 deletions src/controllers/LogViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function index()
'files' => $this->log_viewer->getFiles(true),
'current_file' => $this->log_viewer->getFileName(),
'standardFormat' => true,
'structure' => $this->log_viewer->foldersAndFiles(),
'storage_path' => $this->log_viewer->getStoragePath(),

];

if ($this->request->wantsJson()) {
Expand Down
21 changes: 8 additions & 13 deletions src/views/log.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
.nowrap {
white-space: nowrap;
}
.list-group {
padding: 5px;
}
Expand Down Expand Up @@ -180,19 +184,10 @@ function resetTheme() {
<div class="list-group div-scroll">
@foreach($folders as $folder)
<div class="list-group-item">
<a href="?f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}">
<span class="fa fa-folder"></span> {{$folder}}
</a>
@if ($current_folder == $folder)
<div class="list-group folder">
@foreach($folder_files as $file)
<a href="?l={{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}&f={{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}"
class="list-group-item @if ($current_file == $file) llv-active @endif">
{{$file}}
</a>
@endforeach
</div>
@endif
<?php
\Rap2hpoutre\LaravelLogViewer\LaravelLogViewer::DirectoryTreeStructure( $storage_path, $structure );
?>

</div>
@endforeach
@foreach($files as $file)
Expand Down
Loading

0 comments on commit ce5b1d0

Please sign in to comment.