Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Jul 16, 2018
1 parent 0edf84b commit a8febf2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 52 deletions.
74 changes: 24 additions & 50 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Rap2hpoutre\LaravelLogViewer;

/**
Expand All @@ -17,50 +18,23 @@ class LaravelLogViewer
*/
private $folder;

private $levels_classes = [
'debug' => 'info',
'info' => 'info',
'notice' => 'info',
'warning' => 'warning',
'error' => 'danger',
'critical' => 'danger',
'alert' => 'danger',
'emergency' => 'danger',
'processed' => 'info',
'failed' => 'warning',
];

private $levels_imgs = [
'debug' => 'info-circle',
'info' => 'info-circle',
'notice' => 'info-circle',
'warning' => 'exclamation-triangle',
'error' => 'exclamation-triangle',
'critical' => 'exclamation-triangle',
'alert' => 'exclamation-triangle',
'emergency' => 'exclamation-triangle',
'processed' => 'info-circle',
'failed' => 'exclamation-triangle'
];
/**
* Why? Uh... Sorry
*/
const MAX_FILE_SIZE = 52428800;

/**
* Log levels that are used
* @var array
* @var Level level
*/
private $log_levels = [
'emergency',
'alert',
'critical',
'error',
'warning',
'notice',
'info',
'debug',
'processed',
'failed'
];

const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry
private $level;

/**
* LaravelLogViewer constructor.
*/
public function __construct()
{
$this->level = new Level();
}

/**
* @param string $folder
Expand Down Expand Up @@ -95,8 +69,8 @@ public function setFile($file)
public function pathToLogFile($file)
{
$logsPath = storage_path('logs');
$logsPath .= ($this->folder) ? '/' . $this->folder : '' ;
$logsPath .= ($this->folder) ? '/' . $this->folder : '';

if (app('files')->exists($file)) { // try the absolute path
return $file;
}
Expand Down Expand Up @@ -138,7 +112,7 @@ public function all()

if (!$this->file) {
$log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
if(!count($log_file)) {
if (!count($log_file)) {
return [];
}
$this->file = $log_file[0];
Expand All @@ -161,8 +135,8 @@ public function all()
}

foreach ($headings as $h) {
for ($i=0, $j = count($h); $i < $j; $i++) {
foreach ($this->log_levels as $level) {
for ($i = 0, $j = count($h); $i < $j; $i++) {
foreach ($this->level->all() as $level) {
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {

preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current);
Expand All @@ -171,8 +145,8 @@ public function all()
$log[] = array(
'context' => $current[3],
'level' => $level,
'level_class' => $this->levels_classes[$level],
'level_img' => $this->levels_imgs[$level],
'level_class' => $this->level->cssClass($level),
'level_img' => $this->level->img($level),
'date' => $current[1],
'text' => $current[4],
'in_file' => isset($current[5]) ? $current[5] : null,
Expand All @@ -188,13 +162,13 @@ public function all()
$lines = explode(PHP_EOL, $file);
$log = [];

foreach($lines as $key => $line) {
foreach ($lines as $key => $line) {
$log[] = [
'context' => '',
'level' => '',
'level_class' => '',
'level_img' => '',
'date' => $key+1,
'date' => $key + 1,
'text' => $line,
'in_file' => null,
'stack' => '',
Expand Down
68 changes: 68 additions & 0 deletions src/Rap2hpoutre/LaravelLogViewer/Level.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Rap2hpoutre\LaravelLogViewer;

/**
* Class Level
* @package Rap2hpoutre\LaravelLogViewer
*/
class Level
{
/**
* @var array
*/
private $levels_classes = [
'debug' => 'info',
'info' => 'info',
'notice' => 'info',
'warning' => 'warning',
'error' => 'danger',
'critical' => 'danger',
'alert' => 'danger',
'emergency' => 'danger',
'processed' => 'info',
'failed' => 'warning',
];

/**
* @var array
*/
private $levels_imgs = [
'debug' => 'info-circle',
'info' => 'info-circle',
'notice' => 'info-circle',
'warning' => 'exclamation-triangle',
'error' => 'exclamation-triangle',
'critical' => 'exclamation-triangle',
'alert' => 'exclamation-triangle',
'emergency' => 'exclamation-triangle',
'processed' => 'info-circle',
'failed' => 'exclamation-triangle'
];

/**
* @return array
*/
public function all()
{
return array_keys($this->levels_imgs);
}

/**
* @param $level
* @return string
*/
public function img($level)
{
return $this->levels_imgs[$level];
}

/**
* @param $level
* @return string
*/
public function cssClass($level)
{
return $this->levels_classes[$level];
}
}
6 changes: 4 additions & 2 deletions src/controllers/LogViewerController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Rap2hpoutre\LaravelLogViewer;

use Illuminate\Support\Facades\Crypt;

if (class_exists("\\Illuminate\\Routing\\Controller")) {
Expand All @@ -26,7 +28,7 @@ class LogViewerController extends BaseController
/**
* LogViewerController constructor.
*/
public function __construct ()
public function __construct()
{
$this->log_viewer = new LaravelLogViewer();
$this->request = app('request');
Expand Down Expand Up @@ -88,7 +90,7 @@ private function earlyReturn()
app('files')->delete($this->pathFromInput('del'));
return $this->redirect($this->request->url());
} elseif ($this->request->has('delall')) {
foreach($this->log_viewer->getFiles(true) as $file){
foreach ($this->log_viewer->getFiles(true) as $file) {
app('files')->delete($this->log_viewer->pathToLogFile($file));
}
return $this->redirect($this->request->url());
Expand Down

0 comments on commit a8febf2

Please sign in to comment.