Compare commits

...

15 Commits

Author SHA1 Message Date
zhouyangyang
7e74ae6b0f namespace 2020-11-26 14:09:57 +08:00
zhouyangyang
c57f7ff2f9 namespace 2020-11-26 14:02:44 +08:00
zhouyangyang
d2a4f06b6a title 2020-11-25 13:33:50 +08:00
zhouyangyang
5c801cdb27 忽略 2020-11-25 11:42:34 +08:00
zhouyangyang
d8006be928 忽略 2020-11-25 11:37:43 +08:00
zhouyangyang
7995091b38 md 2020-11-25 11:34:31 +08:00
zhouyangyang
8224a1f704 cdn 2020-11-25 11:28:00 +08:00
zhouyangyang
d43ee5a6bd cdn 2020-11-25 11:21:51 +08:00
Jiang Qinghua
313a1c5c8e
Update LogViewer.php 2020-07-14 22:45:27 +08:00
jqh
eb0d72ffc8 wip 2020-07-14 16:32:35 +08:00
jqh
5241eaa11b wip 2020-07-14 11:26:40 +08:00
jqh
9f9bf0af6c 兼容软连接目录设置 2020-07-13 14:43:29 +08:00
jqh
d65ff46fef wip 2020-07-13 11:55:39 +08:00
jqh
5396f8d6de wip 2020-07-13 11:53:27 +08:00
jqh
317d46abea wip download 2020-07-13 11:06:51 +08:00
9 changed files with 116 additions and 98 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

View File

@ -1,20 +1,3 @@
<div align="center">
# Dcat Laravel Log Viewer
<p>
<a href="https://github.com/jqhph/laravel-log-viewer/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-7389D8.svg?style=flat" ></a>
<a href="https://styleci.io/repos/215738797">
<img src="https://github.styleci.io/repos/215738797/shield" alt="StyleCI">
</a>
<a href="https://github.com/jqhph/laravel-log-viewer/releases" ><img src="https://img.shields.io/github/release/jqhph/laravel-log-viewer.svg?color=4099DE" /></a>
</p>
`Dcat Log Viewer`是一个`Laravel`日志查看工具,支持大文件日志的查看和搜索功能,更改自[laravel-admin-extensions/log-viewer](https://github.com/laravel-admin-extensions/log-viewer)。
</div>
![](https://cdn.learnku.com/uploads/images/202007/09/38389/5Ps3bfhdrR.png!large)
## 功能 ## 功能
@ -36,16 +19,16 @@
## 安装 ## 安装
```bash ```bash
composer require dcat/laravel-log-viewer composer require zyimm/laravel-log-viewer
``` ```
发布配置文件,此步骤可省略 发布配置文件,此步骤可省略
```bash ```bash
php artisan vendor:publish --tag=dcat-log-viewer php artisan vendor:publish --tag=zyimm-log-viewer
``` ```
然后访问 `http://hostname/dcat-logs` 即可 然后访问 `http://hostname/zyimm-logs` 即可
配置文件 配置文件
@ -54,9 +37,9 @@ php artisan vendor:publish --tag=dcat-log-viewer
return [ return [
'route' => [ 'route' => [
// 路由前缀 // 路由前缀
'prefix' => 'dcat-logs', 'prefix' => 'zyimm-logs',
// 命名空间 // 命名空间
'namespace' => 'Dcat\LogViewer', 'namespace' => 'Zyimm\LogViewer',
// 中间件 // 中间件
'middleware' => [], 'middleware' => [],
], ],

View File

@ -1,14 +1,14 @@
{ {
"name": "dcat/laravel-log-viewer", "name": "zyimm/laravel-log-viewer",
"description": "Laravel Log Viewer", "description": "Laravel Log Viewer",
"type": "library", "type": "library",
"keywords": ["laravel", "log viewer"], "keywords": ["laravel", "log viewer"],
"homepage": "https://github.com/jqhph/laravel-log-viewer", "homepage": "http://gogs.zyimm.com/zyimm/laravel-log-viewer",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
"name": "jqh", "name": "zyimm",
"email": "841324345@qq.com" "email": "zyimm@qq.com"
} }
], ],
"require": { "require": {
@ -16,13 +16,13 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Dcat\\LogViewer\\": "src/" "Zyimm\\LogViewer\\": "src/"
} }
}, },
"extra": { "extra": {
"laravel": { "laravel": {
"providers": [ "providers": [
"Dcat\\LogViewer\\DcatLogViewerServiceProvider" "Zyimm\\LogViewer\\LogViewerServiceProvider"
] ]
} }
} }

View File

@ -2,8 +2,8 @@
return [ return [
'route' => [ 'route' => [
'prefix' => 'dcat-logs', 'prefix' => 'zyimm-logs',
'namespace' => 'Dcat\LogViewer', 'namespace' => 'Zyimm\LogViewer',
'middleware' => [], 'middleware' => [],
], ],

View File

@ -5,13 +5,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Laravel log viewer</title> <title>Laravel log viewer</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/html5shiv/r29/html5.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]--> <![endif]-->
<style> <style>
body { body {
@ -27,7 +26,7 @@
h3 { h3 {
font-weight: 400; font-weight: 400;
font-size: 20px; font-size: 18px;
margin-bottom: 0; margin-bottom: 0;
} }
@ -215,7 +214,7 @@
} }
.nav>li>a.dir { .nav>li>a.dir {
font-size: 1.1rem; font-size: 1rem;
} }
</style> </style>
@ -228,25 +227,25 @@
<div class="col-md-2"> <div class="col-md-2">
<div class="logo"> <div class="logo">
Dcat Log Viewer Zyimm Log Viewer
</div> </div>
<div class=""> <div class="">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title"><i class="fa fa-folder-open-o"></i> <h3 class="box-title"><i class="fa fa-folder-open-o"></i>
<a href="{{ route('dcat-log-viewer') }}">logs</a> <a href="{{ route('zyimm-log-viewer') }}">logs</a>
@if($dir) @if($dir)
@php($tmp = '') @php($tmp = '')
@foreach(explode('/', $dir) as $v) @foreach(explode('/', $dir) as $v)
@php($tmp .= '/'.$v) @php($tmp .= '/'.$v)
/ /
<a href="{{ route('dcat-log-viewer', ['dir' => trim($tmp, '/')])}}">{{ $v }}</a> <a href="{{ route('zyimm-log-viewer', ['dir' => trim($tmp, '/')])}}">{{ $v }}</a>
@endforeach @endforeach
@endif @endif
</h3> </h3>
</div> </div>
<form action="{{ route('dcat-log-viewer') }}" style="display: inline-block;width: 220px;padding-left: 15px"> <form action="{{ route('zyimm-log-viewer') }}" style="display: inline-block;width: 220px;padding-left: 15px">
<div class="input-group-sm" style="display: inline-block;width: 100%"> <div class="input-group-sm" style="display: inline-block;width: 100%">
<input name="filename" class="form-control" value="{{ app('request')->get('filename') }}" type="text" placeholder="Search..." /> <input name="filename" class="form-control" value="{{ app('request')->get('filename') }}" type="text" placeholder="Search..." />
</div> </div>
@ -257,8 +256,8 @@
@if(! app('request')->get('filename')) @if(! app('request')->get('filename'))
@foreach($logDirs as $d) @foreach($logDirs as $d)
<li @if($d === $fileName) class="active" @endif> <li @if($d === $fileName) class="active" @endif>
<a class="dir" href="{{ route('dcat-log-viewer', ['dir' => $d]) }}"> <a class="dir" href="{{ route('zyimm-log-viewer', ['dir' => $d]) }}">
<i class="fa fa-folder-o"></i>{{ $d }} <i class="fa fa-folder-o"></i>{{ basename($d) }}
</a> </a>
</li> </li>
@endforeach @endforeach
@ -284,9 +283,9 @@
<div class="col-md-10"> <div class="col-md-10">
<div class="box box-primary"> <div class="box box-primary">
<div class="box-header with-border"> <div class="box-header with-border">
<a href="{{ route('dcat-log-viewer.download', ['dir' => $dir, 'file' => $fileName]) }}" class="btn btn-primary btn-sm download" style="color: #fff"><i class="fa-download fa"></i> {{ trans('Download') }}</a> <a href="{{ route('zyimm-log-viewer.download', ['dir' => $dir, 'file' => $fileName, 'filename' => app('request')->get('filename')]) }}" class="btn btn-primary btn-sm download" style="color: #fff"><i class="fa-download fa"></i> {{ trans('Download') }}</a>
{{-- <button class="btn btn-default btn-sm download"><i class="fa-trash-o fa"></i> {{ trans('Delete') }}</button>--}} {{-- <button class="btn btn-default btn-sm download"><i class="fa-trash-o fa"></i> {{ trans('Delete') }}</button>--}}
&nbsp; &nbsp;
<form action="{{ app('request')->fullUrlWithQuery(['keyword' => null,]) }}" style="display: inline-block;width: 180px"> <form action="{{ app('request')->fullUrlWithQuery(['keyword' => null,]) }}" style="display: inline-block;width: 180px">
<div class="input-group-sm" style="display: inline-block;width: 100%"> <div class="input-group-sm" style="display: inline-block;width: 100%">
@ -333,7 +332,7 @@
@foreach($logs as $index => $log) @foreach($logs as $index => $log)
<tr> <tr>
<td>{{ $index + 1 }}</td> <td>{{ $index + 1 }}</td>
<td><span class="label bg-{{\Dcat\LogViewer\LogViewer::$levelColors[$log['level']]}}">{{ $log['level'] }}</span></td> <td><span class="label bg-{{\Zyimm\LogViewer\LogViewer::$levelColors[$log['level']]}}">{{ $log['level'] }}</span></td>
<td><strong>{{ $log['env'] }}</strong></td> <td><strong>{{ $log['env'] }}</strong></td>
<td style="width:150px;">{{ $log['time'] }}</td> <td style="width:150px;">{{ $log['time'] }}</td>
<td><pre>{{ $log['info'] }}</pre></td> <td><pre>{{ $log['info'] }}</pre></td>
@ -388,7 +387,6 @@
<!-- jQuery for Bootstrap --> <!-- jQuery for Bootstrap -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
{{--<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>--}} {{--<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>--}}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.bundle.min.js"></script>
</body> </body>
</html> </html>

View File

@ -1,32 +0,0 @@
<?php
namespace Dcat\LogViewer;
use Illuminate\Support\ServiceProvider;
class DcatLogViewerServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/view', 'dcat-log-viewer');
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__.'/../config' => config_path()], 'dcat-log-viewer');
}
$this->registerRoutes();
}
protected function registerRoutes()
{
app('router')->group([
'prefix' => config('dcat-log-viewer.route.prefix', 'dcat-logs'),
'namespace' => config('dcat-log-viewer.route.namespace', 'Dcat\LogViewer'),
'middleware' => config('dcat-log-viewer.route.middleware'),
], function ($router) {
$router->get('/', ['as' => 'dcat-log-viewer', 'uses' => 'LogController@index',]);
$router->get('{file}', ['as' => 'dcat-log-viewer.file', 'uses' => 'LogController@index',]);
$router->get('download/{file}', ['as' => 'dcat-log-viewer.download', 'uses' => 'LogController@download',]);
});
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Dcat\LogViewer; namespace Zyimm\LogViewer;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -14,14 +14,14 @@ class LogController
$filename = trim($request->get('filename')); $filename = trim($request->get('filename'));
$offset = $request->get('offset'); $offset = $request->get('offset');
$keyword = trim($request->get('keyword')); $keyword = trim($request->get('keyword'));
$lines = $keyword ? (config('dcat-log-viewer.search_page_items') ?: 500) : (config('dcat-log-viewer.page_items') ?: 30); $lines = $keyword ? (config('zyimm-log-viewer.search_page_items') ?: 500) : (config('zyimm-log-viewer.page_items') ?: 30);
$viewer = new LogViewer($this->getDirectory(), $dir, $file); $viewer = new LogViewer($this->getDirectory(), $dir, $file);
$viewer->setKeyword($keyword); $viewer->setKeyword($keyword);
$viewer->setFilename($filename); $viewer->setFilename($filename);
return view('dcat-log-viewer::log', [ return view('zyimm-log-viewer::log', [
'dir' => $dir, 'dir' => $dir,
'logs' => $viewer->fetch($offset, $lines), 'logs' => $viewer->fetch($offset, $lines),
'logFiles' => $this->formatLogFiles($viewer, $dir), 'logFiles' => $this->formatLogFiles($viewer, $dir),
@ -35,16 +35,26 @@ class LogController
]); ]);
} }
public function download($file = null) public function download()
{ {
$viewer = new LogViewer($this->getDirectory(), request('dir'), $file); $request = app('request');
$file = trim($request->get('file'));
$dir = trim($request->get('dir'));
$filename = trim($request->get('filename'));
$keyword = trim($request->get('keyword'));
$viewer = new LogViewer($this->getDirectory(), $dir, $file);
$viewer->setKeyword($keyword);
$viewer->setFilename($filename);
return response()->download($viewer->getFilePath()); return response()->download($viewer->getFilePath());
} }
protected function getDirectory() protected function getDirectory()
{ {
return config('dcat-log-viewer.directory') ?: storage_path('logs'); return config('zyimm-log-viewer.directory') ?: storage_path('logs');
} }
protected function formatLogFiles(LogViewer $logViewer, $currentDir) protected function formatLogFiles(LogViewer $logViewer, $currentDir)
@ -63,7 +73,7 @@ class LogController
return [ return [
'file' => $value, 'file' => $value,
'url' => route('dcat-log-viewer.file', ['file' => $file, 'dir' => $dir]), 'url' => route('zyimm-log-viewer.file', ['file' => $file, 'dir' => $dir]),
'active' => $logViewer->isCurrentFile($value), 'active' => $logViewer->isCurrentFile($value),
]; ];
}, $logViewer->getLogFiles()); }, $logViewer->getLogFiles());

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Dcat\LogViewer; namespace Zyimm\LogViewer;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -73,12 +73,38 @@ class LogViewer
*/ */
public function __construct($basePath, $dir, $file = null) public function __construct($basePath, $dir, $file = null)
{ {
$this->basePath = rtrim($basePath, '/'); $this->basePath = $this->getRealPath(rtrim($basePath, '/'));
$this->currentDirectory = rtrim($dir, '/'); $this->currentDirectory = $this->formatPath(rtrim($dir, '/'));
$this->file = $file; $this->file = $this->formatPath($file);
$this->files = new Filesystem(); $this->files = new Filesystem();
} }
protected function getRealPath($path)
{
try {
$paths = explode('/', $path);
$result = '';
foreach ($paths as $v) {
$result .= $v.'/';
$current = rtrim($result, '/');
if (is_link($current)) {
$result = readlink($current).'/';
}
}
return rtrim($result, '/');
} catch (\Throwable $e) {
return $path;
}
}
protected function formatPath($path)
{
return str_replace(['../'], '', $path);
}
/** /**
* Get file path by giving log file name. * Get file path by giving log file name.
* *
@ -103,7 +129,7 @@ class LogViewer
public function setFilename($value) public function setFilename($value)
{ {
$this->filename = $value; $this->filename = $this->formatPath($value);
} }
/** /**
@ -136,10 +162,10 @@ class LogViewer
} }
$files = glob($this->mergeDirectory().'/*.*'); $files = glob($this->mergeDirectory().'/*.*');
$files = array_combine($files, array_map('filemtime', $files)); //$files = array_combine($files, array_map('filemtime', $files));
arsort($files); rsort($files);
return array_map('basename', array_keys($files)); return array_map('basename', $files);
} }
public function getLogDirectories() public function getLogDirectories()
@ -160,7 +186,7 @@ class LogViewer
return $this->getLogBasePath(); return $this->getLogBasePath();
} }
return $this->getLogBasePath() . '/' . $this->currentDirectory; return $this->getLogBasePath().'/'.$this->currentDirectory;
} }
/** /**
@ -210,7 +236,7 @@ class LogViewer
return false; return false;
} }
return route('dcat-log-viewer.file', [ return route('zyimm-log-viewer.file', [
'file' => $this->getFile(), 'file' => $this->getFile(),
'offset' => $this->pageOffset['end'], 'offset' => $this->pageOffset['end'],
'keyword' => $this->keyword, 'keyword' => $this->keyword,
@ -234,7 +260,7 @@ class LogViewer
return false; return false;
} }
return route('dcat-log-viewer.file', [ return route('zyimm-log-viewer.file', [
'file' => $this->getFile(), 'file' => $this->getFile(),
'offset' => -$this->pageOffset['start'], 'offset' => -$this->pageOffset['start'],
'keyword' => $this->keyword, 'keyword' => $this->keyword,

View File

@ -0,0 +1,32 @@
<?php
namespace Zyimm\LogViewer;
use Illuminate\Support\ServiceProvider;
class LogViewerServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/view', 'zyimm-log-viewer');
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__.'/../config' => config_path()], 'zyimm-log-viewer');
}
$this->registerRoutes();
}
protected function registerRoutes()
{
app('router')->group([
'prefix' => config('zyimm-log-viewer.route.prefix', 'zyimm-logs'),
'namespace' => config('zyimm-log-viewer.route.namespace', 'Zyimm\LogViewer'),
'middleware' => config('zyimm-log-viewer.route.middleware'),
], function ($router) {
$router->get('/', ['as' => 'zyimm-log-viewer', 'uses' => 'LogController@index',]);
$router->get('download', ['as' => 'zyimm-log-viewer.download', 'uses' => 'LogController@download',]);
$router->get('{file}', ['as' => 'zyimm-log-viewer.file', 'uses' => 'LogController@index',]);
});
}
}