Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
fix large file reading.
Browse files Browse the repository at this point in the history
fix large file reading.
  • Loading branch information
tanwencn committed Apr 26, 2020
1 parent 23d06ab commit 6329d5d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docusaurus/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ pjax内容过滤中间件,删除这个中间件,会自动取消pjax功能

### logger.except
不需要记录的字段。

## Laravel Logs
后台查看Laravel 日志
### laravel_logs.read_once_rows
一页读取多少行日志
7 changes: 6 additions & 1 deletion resources/views/_log_views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
<h3 class="box-title">Logs</h3>

<div class="box-tools">
<div class="input-group input-group-sm" style="width: 150px;">
<div class="input-group input-group-sm" style="width: 260px;">
@if($eof)
<div class="input-group-btn">
<a class="btn btn-sm btn-default" href="{{ Admin::action('index', array_merge(request()->query(), ['page' => $page+1, 'timestrap' => time()])) }}">@lang('pagination.next')</a>
</div>
@endif
<input type="search" name="search" class="form-control pull-right" value=""
placeholder="Search...">

Expand Down
53 changes: 42 additions & 11 deletions src/Http/Controllers/LogViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
use Illuminate\Support\Arr;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Finder\Finder;
use Illuminate\Support\Facades\URL;

class LogViewController extends Controller
{
use Package;

protected $rows_line = 0;
protected $eof = false;

public function index(Request $request)
{
$current = $request->filled('f')?decrypt($request->query('f'), false):"";
$page = $request->query('page', 1);
$current = $request->filled('f') ? decrypt($request->query('f'), false) : "";
if ($page == 1) $this->rows_key = 0;
$current_file = null;
$tree = [];
$log_path = storage_path('logs');
Expand All @@ -35,30 +41,50 @@ public function index(Request $request)
$pathname = $file->getRelativePathname();
$info = pathinfo($pathname);
$key = $info['dirname'] == '.' ? $info['filename'] : str_replace('/', '.', $info['dirname']) . '.' . $info['filename'];
$fileName = $log_path.'/'.$pathname;
$fileName = $log_path . '/' . $pathname;

/*if(is_file($fileName) && !$current)
$current = $fileName;*/

if($current == $fileName)
if ($current == $fileName)
$current_file = $file;

Arr::set($tree, $key, $fileName);
}

$statistics = "";

$data = $current?$this->data($current_file):[];
if ($page == 1) session(['log_view_line' => 0]);
$this->rows_line = $this->isRefresh()?session('log_view_line_last'):session('log_view_line', 0);

return $this->view('index', compact('tree', 'statistics', 'current', 'data'));
}
$data = $current ? $this->data($current_file) : [];

protected function data(SplFileInfo $file){
/*if (app('files')->size($file) >= 15*1024*1024) {
return null;
}*/
$eof = $this->eof;

$content = $file->getContents();
return $this->view('index', compact('tree', 'statistics', 'current', 'data', 'page', 'eof'));
}

protected function data(SplFileInfo $file)
{
$file = (new \SplFileObject($file));
$file->seek($this->rows_line);
$once_rows = 0;
$content = "";

while ($this->eof = $file->valid()) {
$line = $file->fgets() . "\n";
if ($once_rows > config('admin.laravel_logs.read_once_rows', 10000) && preg_match('/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/', $line)) {
if (!$this->isRefresh()) {
session(['log_view_line_last' => $this->rows_line]);
session(['log_view_line' => $file->key()-1]);
session(['log_view_line_time' => request()->query('timestrap')]);
}
break;
};

$content .= $line;
$once_rows++;
}

preg_match_all('/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/', $content, $headings);
$log_data = preg_split('/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/', $content, null);
Expand All @@ -75,4 +101,9 @@ protected function data(SplFileInfo $file){

return array_reverse($results);
}

private function isRefresh()
{
return request()->filled('page') && request()->filled('timestrap') && request()->query('timestrap') == session('log_view_line_time');
}
}

0 comments on commit 6329d5d

Please sign in to comment.