Skip to content

修复无法识别 logs文件夹下用子文件夹存储日志 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BootExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected static function registerRoutes()
parent::routes(function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->get('logs', 'Encore\Admin\LogViewer\LogController@index')->name('log-viewer-index');
$router->get('logs/{file}', 'Encore\Admin\LogViewer\LogController@index')->name('log-viewer-file');
$router->get('logs/{file}/tail', 'Encore\Admin\LogViewer\LogController@tail')->name('log-viewer-tail');
$router->get('log', 'Encore\Admin\LogViewer\LogController@index')->name('log-viewer-file');
$router->get('log/tail', 'Encore\Admin\LogViewer\LogController@tail')->name('log-viewer-tail');
});
}

Expand Down
13 changes: 8 additions & 5 deletions src/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

class LogController extends Controller
{
public function index($file = null, Request $request)
public function index(Request $request)
{
if ($file === null) {
if (!$request->query('file')) {
$file = (new LogViewer())->getLastModifiedLog();
}

}else{
$file = $request->query('file');
}

return Admin::content(function (Content $content) use ($file, $request) {
$offset = $request->get('offset');
Expand All @@ -36,10 +39,10 @@ public function index($file = null, Request $request)
});
}

public function tail($file, Request $request)
public function tail(Request $request)
{
$file = $request->query('file');
$offset = $request->get('offset');

$viewer = new LogViewer($file);

list($pos, $logs) = $viewer->tail($offset);
Expand Down
34 changes: 30 additions & 4 deletions src/LogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,41 @@ public function getFilesize()
public function getLogFiles($count = 20)
{
$files = glob(storage_path('logs/*'));
$files = $this->getFiles(storage_path('logs'));
//递归获取所有文件

$files = array_combine($files, array_map('filemtime', $files));
arsort($files);

$files = array_map('basename', array_keys($files));

$files = array_map(array($this,'getName'), array_keys($files));
return array_slice($files, 0, $count);
}

//自定义递归方法获取 当前文件夹下所有 文件: Array
private function getFiles($directory){
$files = glob("$directory/*");

$array = array();
foreach($files as $file){
if(is_file($file)){
$array[] = $file;
continue;
}
if(is_dir($file)){
$array = array_merge($array,$this->getFiles($file));
}
}
return $array;
}

private function getName($file){
$array = explode('/logs/',$file);

if(count($array)>1){
return $array[1];
}
return $file;
}
/**
* Get the last modified log file.
*
Expand All @@ -120,7 +147,6 @@ public function getLogFiles($count = 20)
public function getLastModifiedLog()
{
$logs = $this->getLogFiles();

return current($logs);
}

Expand Down