Skip to content

Commit

Permalink
Merge pull request #288 from ARCANEDEV/patch-6
Browse files Browse the repository at this point in the history
[4.3] Fixing the log checker and compact() error
  • Loading branch information
arcanedev-maroc authored Sep 12, 2019
2 parents d7df65b + d3a22b5 commit 4aa7732
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
.travis.yml export-ignore
CONTRIBUTING.md export-ignore
CODE_OF_CONDUCT.md export-ignore
phpunit.xml export-ignore
phpunit.xml.dist export-ignore
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

sudo: false

php:
- 5.6
- 7.0
Expand All @@ -24,5 +22,5 @@ script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"require": {
"php": ">=5.6",
"ext-json": "*",
"psr/log": "~1.0",
"arcanedev/support": "~4.1.0"
},
Expand Down
30 changes: 21 additions & 9 deletions resources/views/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<?php
/**
* @var Arcanedev\LogViewer\Entities\Log $log
* @var Illuminate\Pagination\LengthAwarePaginator $entries
* @var string|null $query
*/
?>
@extends('log-viewer::_template.master')

@section('content')
Expand Down Expand Up @@ -57,12 +64,16 @@
<form action="{{ route('log-viewer::logs.search', [$log->date, $level]) }}" method="GET">
<div class=form-group">
<div class="input-group">
<input id="query" name="query" class="form-control" value="{!! request('query') !!}" placeholder="typing something to search">
<input id="query" name="query" class="form-control" value="{!! $query !!}" placeholder="typing something to search">
<span class="input-group-btn">
@if (request()->has('query'))
<a href="{{ route('log-viewer::logs.show', [$log->date]) }}" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span></a>
@endif
<button id="search-btn" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span></button>
@unless (is_null($query))
<a href="{{ route('log-viewer::logs.show', [$log->date]) }}" class="btn btn-default">
<span class="glyphicon glyphicon-remove"></span>
</a>
@endunless
<button id="search-btn" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
Expand All @@ -74,10 +85,10 @@
<div class="panel panel-default">
@if ($entries->hasPages())
<div class="panel-heading">
{!! $entries->appends(compact('query'))->render() !!}
{{ $entries->appends(compact('query'))->render() }}
<span class="label label-info pull-right">
Page {!! $entries->currentPage() !!} of {!! $entries->lastPage() !!}
Page {{ $entries->currentPage() }} of {{ $entries->lastPage() }}
</span>
</div>
@endif
Expand All @@ -95,6 +106,7 @@
</thead>
<tbody>
@forelse($entries as $key => $entry)
<?php /** @var Arcanedev\LogViewer\Entities\LogEntry $entry */ ?>
<tr>
<td>
<span class="label label-env">{{ $entry->env }}</span>
Expand Down Expand Up @@ -142,10 +154,10 @@

@if ($entries->hasPages())
<div class="panel-footer">
{!! $entries->appends(compact('query'))->render() !!}
{{ $entries->appends(compact('query'))->render() }}

<span class="label label-info pull-right">
Page {!! $entries->currentPage() !!} of {!! $entries->lastPage() !!}
Page {{ $entries->currentPage() }} of {{ $entries->lastPage() }}
</span>
</div>
@endif
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ private function displayMessages()
$rows[] = [$file, $message];
}

$this->frame('LogViewer messages');
$this->table(['File', 'Message'], $rows);
if ( ! empty($rows)) {
$this->frame('LogViewer messages');
$this->table(['File', 'Message'], $rows);
}
}
}
39 changes: 23 additions & 16 deletions src/Http/Controllers/LogViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
Expand Down Expand Up @@ -90,38 +91,42 @@ public function listLogs(Request $request)
/**
* Show the log.
*
* @param string $date
* @param string $date
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\View\View
*/
public function show($date)
public function show($date, Request $request)
{
$level = 'all';
$log = $this->getLogOrFail($date);
$query = $request->get('query');
$levels = $this->logViewer->levelsNames();
$entries = $log->entries($level = 'all')->paginate($this->perPage);
$entries = $log->entries($level)->paginate($this->perPage);

return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries'));
return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
}

/**
* Filter the log entries by level.
*
* @param string $date
* @param string $level
* @param string $date
* @param string $level
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
*/
public function showByLevel($date, $level)
public function showByLevel($date, $level, Request $request)
{
$log = $this->getLogOrFail($date);

if ($level === 'all')
return redirect()->route($this->showRoute, [$date]);

$log = $this->getLogOrFail($date);
$query = $request->get('query');
$levels = $this->logViewer->levelsNames();
$entries = $this->logViewer->entries($date, $level)->paginate($this->perPage);

return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries'));
return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
}

/**
Expand All @@ -133,18 +138,20 @@ public function showByLevel($date, $level)
*
* @return \Illuminate\View\View
*/
public function search($date, $level = 'all', Request $request) {
$log = $this->getLogOrFail($date);
public function search($date, $level = 'all', Request $request)
{
$query = $request->get('query');

if (is_null($query = $request->get('query')))
return redirect()->route('log-viewer::logs.show', [$date]);
if (is_null($query))
return redirect()->route($this->showRoute, [$date]);

$log = $this->getLogOrFail($date);
$levels = $this->logViewer->levelsNames();
$entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) {
return Str::contains($value->header, $query);
})->paginate($this->perPage);

return $this->view('show', compact('log', 'levels', 'level', 'query', 'entries'));
return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
}

/**
Expand Down Expand Up @@ -207,7 +214,7 @@ protected function view($view, $data = [], $mergeData = [])
*/
protected function paginate(array $data, Request $request)
{
$data = collect($data);
$data = new Collection($data);
$page = $request->get('page', 1);
$path = $request->url();

Expand Down
34 changes: 17 additions & 17 deletions src/Utilities/LogChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,23 @@ private function checkLogFiles()
*/
private function checkLogFile($path)
{
$status = true;
$file = basename($path);
$message = "The log file [$file] is valid.";

if ($this->isSingleLogFile($file)) {
$this->status = $status = false;
$this->messages['files'][$file] = $message =
"You have a single log file in your application, you should split the [$file] into seperate log files.";
$status = true;
$filename = basename($path);
$message = "The log file [$filename] is valid.";
$pattern = $this->filesystem->getPattern();

if ($this->isSingleLogFile($filename)) {
$this->status = $status = false;
$this->messages['files'][$filename] = $message =
"You have a single log file in your application, you should split the [$filename] into separate log files.";
}
elseif ($this->isInvalidLogDate($file)) {
$this->status = $status = false;
$this->messages['files'][$file] = $message =
"The log file [$file] has an invalid date, the format must be like laravel-YYYY-MM-DD.log.";
elseif ($this->isInvalidLogPattern($filename, $pattern)) {
$this->status = $status = false;
$this->messages['files'][$filename] = $message =
"The log file [$filename] has an invalid date, the format must be like {$pattern}.";
}

$this->files[$file] = compact('filename', 'status', 'message', 'path');
$this->files[$filename] = compact('filename', 'status', 'message', 'path');
}

/**
Expand All @@ -299,13 +300,12 @@ private function isSingleLogFile($file)
* Check the date of the log file.
*
* @param string $file
* @param string $pattern
*
* @return bool
*/
private function isInvalidLogDate($file)
private function isInvalidLogPattern($file, $pattern)
{
$pattern = '/laravel-(\d){4}-(\d){2}-(\d){2}.log/';

return ((bool) preg_match($pattern, $file, $matches)) === false;
return ((bool) preg_match("/{$pattern}/", $file, $matches)) === false;
}
}

0 comments on commit 4aa7732

Please sign in to comment.