Rebuild logs view
parent
64dc16e4d9
commit
e947e788f9
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Engelsystem\Models\LogEntry;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function admin_log_title()
|
||||
{
|
||||
return __('Log');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function admin_log()
|
||||
{
|
||||
$filter = '';
|
||||
if (request()->has('keyword')) {
|
||||
$filter = strip_request_item('keyword');
|
||||
}
|
||||
|
||||
$log_entries = LogEntry::filter($filter);
|
||||
|
||||
$entries = [];
|
||||
foreach ($log_entries as $entry) {
|
||||
$data = $entry->toArray();
|
||||
$data['message'] = nl2br(htmlspecialchars($data['message']));
|
||||
$data['created_at'] = date_format($entry->created_at, 'd.m.Y H:i');
|
||||
$entries[] = $data;
|
||||
}
|
||||
|
||||
return page_with_title(admin_log_title(), [
|
||||
msg(),
|
||||
form([
|
||||
form_text('keyword', __('Search'), $filter),
|
||||
form_submit(__('Search'), 'Go')
|
||||
]),
|
||||
table([
|
||||
'created_at' => 'Time',
|
||||
'level' => 'Type',
|
||||
'message' => 'Log Entry'
|
||||
], $entries)
|
||||
]);
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
{% extends 'layouts/app.twig' %}
|
||||
{% import 'macros/base.twig' as m %}
|
||||
{% import 'macros/form.twig' as f %}
|
||||
|
||||
{% set title %}{% block title %}{{ __('log.log') }}{% endblock %}{% endset %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-md-12">
|
||||
<h1>{{ block('title') }}</h1>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="inline-form-spacing">
|
||||
<form method="POST" action="{{ url('/admin/logs') }}" class="form-inline">
|
||||
{{ csrf() }}
|
||||
|
||||
{{ f.input('search', __('form.search'), 'text', {'value': search, 'hide_label': true}) }}
|
||||
|
||||
{{ f.submit(__('form.search')) }}
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<table class="table table-striped">
|
||||
<tr>
|
||||
<th>{{ __('log.time') }}</th>
|
||||
<th>{{ __('log.level') }}</th>
|
||||
<th>{{ __('log.message') }}</th>
|
||||
</tr>
|
||||
{% for entry in entries %}
|
||||
{%- set type = 'default' %}
|
||||
{%- if entry.level in ['notice', 'info'] %}
|
||||
{%- set type = 'info' %}
|
||||
{%- endif %}
|
||||
{%- if entry.level in ['error', 'warning'] %}
|
||||
{%- set type = 'warning' %}
|
||||
{%- endif %}
|
||||
{%- if entry.level in ['emergency', 'alert', 'critical'] %}
|
||||
{%- set type = 'danger' %}
|
||||
{%- endif %}
|
||||
|
||||
{%- set td_type = '' %}
|
||||
{%- if type in ['warning', 'danger'] %}
|
||||
{%- set td_type = type %}
|
||||
{%- endif %}
|
||||
|
||||
<tr>
|
||||
<td class="{{ td_type }}">{{ entry.created_at.format(__('Y-m-d H:i')) }}</td>
|
||||
<td class="{{ td_type }}">
|
||||
<span class="label label-{{ type }}">{{ entry.level|capitalize }}</span>
|
||||
</td>
|
||||
<td>{{ entry.message|nl2br }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Controllers\Admin;
|
||||
|
||||
use Engelsystem\Controllers\BaseController;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\LogEntry;
|
||||
|
||||
class LogsController extends BaseController
|
||||
{
|
||||
/** @var LogEntry */
|
||||
protected $log;
|
||||
|
||||
/** @var Response */
|
||||
protected $response;
|
||||
|
||||
/** @var array */
|
||||
protected $permissions = [
|
||||
'admin_log',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param LogEntry $log
|
||||
* @param Response $response
|
||||
*/
|
||||
public function __construct(LogEntry $log, Response $response)
|
||||
{
|
||||
$this->log = $log;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$search = $request->input('search');
|
||||
$entries = $this->log->filter($search);
|
||||
|
||||
return $this->response->withView(
|
||||
'admin/log.twig',
|
||||
['entries' => $entries, 'search' => $search]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Admin;
|
||||
|
||||
use Engelsystem\Controllers\Admin\LogsController;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\LogEntry;
|
||||
use Engelsystem\Test\Unit\HasDatabase;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class LogsControllerTest extends TestCase
|
||||
{
|
||||
use HasDatabase;
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\Admin\LogsController::index
|
||||
* @covers \Engelsystem\Controllers\Admin\LogsController::__construct
|
||||
*/
|
||||
public function testIndex()
|
||||
{
|
||||
$log = new LogEntry();
|
||||
$alert = $log->create(['level' => LogLevel::ALERT, 'message' => 'Alert test']);
|
||||
$alert = $log->find($alert)->first();
|
||||
$error = $log->create(['level' => LogLevel::ERROR, 'message' => 'Error test']);
|
||||
$error = $log->find($error)->first();
|
||||
|
||||
$response = $this->createMock(Response::class);
|
||||
$response->expects($this->exactly(2))
|
||||
->method('withView')
|
||||
->withConsecutive(
|
||||
['admin/log.twig', ['entries' => new Collection([$error, $alert]), 'search' => null]],
|
||||
['admin/log.twig', ['entries' => new Collection([$error]), 'search' => 'error']]
|
||||
)
|
||||
->willReturn($response);
|
||||
|
||||
$request = Request::create('/');
|
||||
|
||||
$controller = new LogsController($log, $response);
|
||||
$controller->index($request);
|
||||
|
||||
$request->request->set('search', 'error');
|
||||
$controller->index($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the DB
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->initDatabase();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue