Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
songzou committed Jul 28, 2017
0 parents commit 3be9720
Show file tree
Hide file tree
Showing 9 changed files with 751 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
phpunit.phar
/vendor
composer.phar
composer.lock
*.project
.idea/
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Jens Segers

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
laravel-admin-ext/log-viewer
============================

## Installation

```
$ composer require laravel-admin-ext/log-viewer -vvv
```

Open `app/Providers/AppServiceProvider.php`, and call the `LogViewer::boot` method within the `boot` method:

```php
<?php

namespace App\Providers;

use Encore\Admin\LogViewer\LogViewer;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot()
{
LogViewer::boot();
}
}
```

At last run:

```
$ php artisan admin:import log-viewer
```

Finally open `http://localhost/admin/logs`.

License
------------
Licensed under [The MIT License (MIT)](LICENSE).
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "laravel-admin-ext/log-viewer",
"description": "Log viewer for laravel",
"type": "library",
"keywords": ["laravel-admin", "log", "viewer"],
"homepage": "https://github.com/laravel-admin-extensions/log-viewer",
"license": "MIT",
"authors": [
{
"name": "z-song",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0.0",
"laravel/framework": "5.5.*",
"encore/laravel-admin": "1.5.*"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"laravel/laravel": "5.*"
},
"autoload": {
"psr-4": {
"Encore\\Admin\\LogViewer\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Encore\\Admin\\LogViewer\\LogViewerServiceProvider"
]

}
}
}
184 changes: 184 additions & 0 deletions resources/views/logs.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<script data-exec-on-popstate xmlns="http://www.w3.org/1999/html">
$(function () {
LA.intervalIds = [];
LA.addIntervalId = function (intervalId, persist) {
this.intervalIds.push({id:intervalId, persist:persist});
};
LA.clearIntervalId = function (intervalId) {
for (var id in this.intervalIds) {
if (intervalId == this.intervalIds[id].id && !this.intervalIds[id].persist) {
clearInterval(intervalId);
this.intervalIds.splice(id, 1);
}
}
};
LA.cleanIntervalId = function () {
for (var id in this.intervalIds) {
if (!this.intervalIds[id].persist) {
clearInterval(this.intervalIds[id].id);
this.intervalIds.splice(id, 1);
}
}
};
$(document).on('pjax:complete', function(xhr) {
$.admin.cleanIntervalId();
});
$('.log-refresh').on('click', function() {
$.pjax.reload('#pjax-container');
});
var pos = {{ $end }};
function changePos(offset){
pos = offset;
}
function fetch() {
$.ajax({
url:'{{ $tailPath }}',
method: 'get',
data : {offset : pos},
success:function(data) {
for (var i in data.logs) {
$('table > tbody > tr:first').before(data.logs[i]);
}
changePos(data.pos);
}
});
}
var refreshIntervalId = null;
$('.log-live').click(function() {
$("i", this).toggleClass("fa-play fa-pause");
if (refreshIntervalId) {
$.admin.clearIntervalId(refreshIntervalId);
refreshIntervalId = null;
} else {
refreshIntervalId = setInterval(function() {
fetch();
}, 2000);
$.admin.addIntervalId(refreshIntervalId, false);
}
});
});
</script>
<div class="row">

<!-- /.col -->
<div class="col-md-10">
<div class="box box-primary">
<div class="box-header with-border">
<button type="button" class="btn btn-primary btn-sm log-refresh"><i class="fa fa-refresh"></i> {{ trans('admin.refresh') }}</button>
<button type="button" class="btn btn-default btn-sm log-live"><i class="fa fa-play"></i> </button>
<div class="pull-right">
<div class="btn-group">
@if ($prevUrl)
<a href="{{ $prevUrl }}" class="btn btn-default btn-sm"><i class="fa fa-chevron-left"></i></a>
@endif
@if ($nextUrl)
<a href="{{ $nextUrl }}" class="btn btn-default btn-sm"><i class="fa fa-chevron-right"></i></a>
@endif
</div>
<!-- /.btn-group -->
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body no-padding">

<div class="table-responsive">
<table class="table table-hover">

<thead>
<tr>
<th>Level</th>
<th>Env</th>
<th>Time</th>
<th>Message</th>
<th></th>
</tr>
</thead>

<tbody>

@foreach($logs as $index => $log)

<tr>
<td><span class="label bg-{{\Encore\Admin\LogViewer\LogViewer::$levelColors[$log['level']]}}">{{ $log['level'] }}</span></td>
<td><strong>{{ $log['env'] }}</strong></td>
<td style="width:150px;">{{ $log['time'] }}</td>
<td><code>{{ $log['info'] }}</code></td>
<td>
@if(!empty($log['trace']))
<a class="btn btn-primary btn-xs" data-toggle="collapse" data-target=".trace-{{$index}}"><i class="fa fa-info"></i>&nbsp;&nbsp;Exception</a>
@endif
</td>
</tr>

@if (!empty($log['trace']))
<tr class="collapse trace-{{$index}}">
<td colspan="5"><div style="white-space: pre-wrap;background: #333;color: #fff; padding: 10px;">{{ $log['trace'] }}</div></td>
</tr>
@endif

@endforeach

</tbody>
</table>
<!-- /.table -->
</div>
<!-- /.mail-box-messages -->
</div>
<!-- /.box-body -->
</div>
<!-- /. box -->
</div>

<div class="col-md-2">

<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Files</h3>
</div>
<div class="box-body no-padding">
<ul class="nav nav-pills nav-stacked">
@foreach($logFiles as $logFile)
<li @if($logFile == $fileName)class="active"@endif>
<a href="{{ action('\Encore\Admin\LogViewer\LogController@index', ['file' => $logFile]) }}"><i class="fa fa-{{ ($logFile == $fileName) ? 'folder-open' : 'folder' }}"></i>{{ $logFile }}</a>
</li>
@endforeach
</ul>
</div>
<!-- /.box-body -->
</div>

<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Info</h3>
</div>
<div class="box-body no-padding">
<ul class="nav nav-pills nav-stacked">
<li class="margin: 10px;">
<a>Size: {{ $size }}</a>
</li>
<li class="margin: 10px;">
<a>Updated at: {{ date('Y-m-d H:i:s', filectime($filePath)) }}</a>
</li>
</ul>
</div>
<!-- /.box-body -->
</div>

<!-- /.box -->
</div>
<!-- /.col -->
</div>
43 changes: 43 additions & 0 deletions src/BootExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Encore\Admin\LogViewer;

use Encore\Admin\Admin;

trait BootExtension
{
/**
* {@inheritdoc}
*/
public static function boot()
{
static::registerRoutes();

Admin::extend('log-viwer', __CLASS__);
}

/**
* Register routes for laravel-admin.
*
* @return void
*/
protected static function registerRoutes()
{
parent::routes(function ($router) {
/* @var \Illuminate\Routing\Router $router */
$router->get('logs', 'Encore\Admin\LogViewer\LogController@index');
$router->get('logs/{file}', 'Encore\Admin\LogViewer\LogController@index');
$router->get('logs/{file}/tail', 'Encore\Admin\LogViewer\LogController@tail');
});
}

/**
* {@inheritdoc}
*/
public static function import()
{
parent::createMenu('Log viwer', 'logs', 'fa-database');

parent::createPermission('Logs', 'ext.log-viwer', 'logs*');
}
}
58 changes: 58 additions & 0 deletions src/LogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Encore\Admin\LogViewer;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class LogController extends Controller
{
public function index($file = null, Request $request)
{
return Admin::content(function (Content $content) use ($file, $request) {

$offset = $request->get('offset');

$viewer = new LogViewer($file);

$content->body(view('laravel-admin-logs::logs', [
'logs' => $viewer->fetch($offset),
'logFiles' => $viewer->getLogFiles(),
'fileName' => $viewer->file,
'end' => $viewer->getFilesize(),
'tailPath' => action('\Encore\Admin\LogViewer\LogController@tail', ['file' => $viewer->file]),
'prevUrl' => $viewer->getPrevPageUrl(),
'nextUrl' => $viewer->getNextPageUrl(),
'filePath' => $viewer->getFilePath(),
'size' => static::bytesToHuman($viewer->getFilesize())
]));

$content->header($viewer->getFilePath());

});
}

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

$viewer = new LogViewer($file);

list($pos, $logs) = $viewer->tail($offset);

return compact('pos', 'logs');
}

protected static function bytesToHuman($bytes)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];

for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}

return round($bytes, 2) . ' ' . $units[$i];
}
}
Loading

0 comments on commit 3be9720

Please sign in to comment.