Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize response to JSON Exception #224

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 219 additions & 37 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Rap2hpoutre\LaravelLogViewer;

use Rap2hpoutre\LaravelLogViewer\Level;
use Rap2hpoutre\LaravelLogViewer\Pattern;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
* Class LaravelLogViewer
* @package Rap2hpoutre\LaravelLogViewer
Expand Down Expand Up @@ -138,7 +143,7 @@ public function getFileName()
/**
* @return array
*/
public function all()
public function all($parseStack = false)
{
$log = array();

Expand All @@ -150,72 +155,249 @@ public function all()
$this->file = $log_file[0];
}

$max_file_size = function_exists('config') ? config('logviewer.max_file_size', self::MAX_FILE_SIZE) : self::MAX_FILE_SIZE;
$max_file_size = function_exists('config') ? config('logviewer.max_file_size', LaravelLogViewer::MAX_FILE_SIZE) : LaravelLogViewer::MAX_FILE_SIZE;
if (app('files')->size($this->file) > $max_file_size) {
return null;
}

$file = app('files')->get($this->file);

preg_match_all($this->pattern->getPattern('logs'), $file, $headings);

return $this->convertStringExceptionLogToArray($file, $parseStack);

}

/**
* Convert the given exception to an array.
*
* @param \Exception $e
* @return array
*/
//https://laravel.com/api/[x.x]/Illuminate/Foundation/Exceptions/Handler.html#method_convertExceptionToArray
public function convertExceptionToArray(\Exception $e)
{
return
// config('app.debug') ?
[
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
// ] : [
// 'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
}

/**
*
* @param String $e
* @return array
*/
public function convertStringExceptionLogToArray(String $e, $parseStack = false)
{
$log = array();

preg_match_all($this->pattern->getPattern('logs'), $e, $headings);

if (!is_array($headings)) {
return $log;
}

$log_data = preg_split($this->pattern->getPattern('logs'), $file);
$log_data = preg_split($this->pattern->getPattern('logs'), $e);

if ($log_data[0] < 1) {
array_shift($log_data);
}

foreach ($headings as $h) {
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 . ':')) {

// $h[$i] = str_replace('\\\\', '\\', $h[$i]);
$current = [];
preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current);
if (!isset($current[4])) {
continue;
preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 2), $h[$i], $current);
if (!$current) {
continue;
}
$log[] = array(
'context' => $current[3],
'level' => $level,
'folder' => $this->folder,
'level_class' => $this->level->cssClass($level),
'level_img' => $this->level->img($level),
'date' => $current[1],
'message' => $current[4]." ".(isset($current[5]) ? $current[5] : ""). " ",
// 'exception' => null,
// 'code' => null,
// 'file' => null,
// 'line' => null,
// 'trace' => null,
);
} else {
// $log_data[$i] = str_replace('\\\\', '\\', $log_data[$i]);
$trace = $log_data[$i];
if ($parseStack) {
$trace = $this->convertStringExceptionStackToArray($log_data[$i]);
}
$log[] = array(
'context' => $current[3],
'level' => $level,
'folder' => $this->folder,
'level_class' => $this->level->cssClass($level),
'level_img' => $this->level->img($level),
'date' => $current[1],
'message' => $current[6],
'exception' => $current[4],
// 'code' => $current[5],
'file' => isset($current[7]) ? $current[7] : null,
'line' => isset($current[8]) ? intval($current[8]) : null,
'trace' => $trace,
);
}

$log[] = array(
'context' => $current[3],
'level' => $level,
'folder' => $this->folder,
'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,
'stack' => preg_replace("/^\n*/", '', $log_data[$i])
);
}
}
}
}

if (empty($log)) {

$lines = explode(PHP_EOL, $file);
$log = [];

foreach ($lines as $key => $line) {
$log[] = [
'context' => '',
'level' => '',
'folder' => '',
'level_class' => '',
'level_img' => '',
'date' => $key + 1,
'text' => $line,
'in_file' => null,
'stack' => '',
];
return array_reverse($log);
}

/**
*
* @param String $e
* @return array
*/
public function convertStringExceptionToArray(String $e)
{
$log = array();

$eArray = preg_split("/\n/", $e);
preg_match($this->pattern->getPattern('current_log_string'), $eArray[0], $current);

$log['message'] = $current[2];
$log['exception'] = $current[1];
$log['file'] = isset($current[3]) ? $current[3] : null;
$log['line'] = isset($current[4]) ? intval($current[4]) : null;
$log['trace'] = $this->convertArrayExceptionStackStringToArray(array_slice($eArray, 2));

return $log;
}

/**
*
* @param String $eStack
* @return array
*/
public function convertStringExceptionStackToArray(String $eStack)
{
$eStack = preg_replace($this->pattern->getPattern('stack_init_section'), '', $eStack);
$eStackArray = preg_split("/\\n *\#/", $eStack);
return $this->convertArrayExceptionStackStringToArray($eStackArray);
}

/**
*
* @param array $eStackArray
* @return array
*/
public function convertArrayExceptionStackStringToArray(array $eStackArray)
{
$eStackArrayReturn = [];

foreach ($eStackArray as $s) {
$s = preg_replace($this->pattern->getPattern('stack_startWith'), '', $s);
if (Str::startsWith($s, "{main}")) {
break;
}
if (Str::startsWith($s, "[internal function]: ")) {
$s = str_replace("[internal function]: ", "", $s);
$call = new \StdClass();
preg_match($this->pattern->getPattern('stack', 0), $s, $matchs);
if ($matchs) {
$call->function = $matchs[3];
$call->class = $matchs[1];
$call->type = $matchs[2];
// $call->args = $matchs[4];
$eStackArrayReturn[] = $call;
} else {
preg_match($this->pattern->getPattern('stack', 1), $s, $matchs);
$call->function = $matchs[1];
// $call->args = $matchs[2];
$eStackArrayReturn[] = $call;
}
} else {
preg_match($this->pattern->getPattern('stack', 2), $s, $matchs);
if ($matchs) {
$call = new \StdClass();
$call->file = $matchs[1];
$call->line = intval($matchs[2]);
$tmp = $matchs[3];
preg_match($this->pattern->getPattern('stack', 0), $tmp, $matchs);
if ($matchs) {
$call->function = $matchs[3];
$call->class = $matchs[1];
$call->type = $matchs[2];
if (isset($matchs[4])) {
// $call->args = $matchs[4];
}
} else {
preg_match($this->pattern->getPattern('stack', 1), $tmp, $matchs);
if ($matchs) {
$call->function = $matchs[1];
}
if (isset($matchs[2])) {
// $call->args = $matchs[2];
}
}
$eStackArrayReturn[] = $call;
} else {
$eStackArrayReturn[] = $s;
}
}
}
return $eStackArrayReturn;
}

return array_reverse($log);
/**
*
* @param array $stackArray
* @return String
*/
public static function convertArrayExceptionStacArrayToString(array $stackArray)
{
$stack = "";
$stack .= "[stacktrace]";
$stack .= "\n";
foreach ($stackArray as $index=>$data) {
$dataArray = (array)$data;
$stack .= "#$index ";
if (isset($dataArray['file'])) {
$stack .= "{$dataArray['file']} ({$dataArray['line']}): ";
}
$strClass = "";
if (isset($dataArray['class'])) {
$strClass = $dataArray['class'];
}
$strType = "";
if (isset($dataArray['type'])) {
$strType = $dataArray['type'];
}
$strFunction = "";
if (isset($dataArray['function'])) {
$strFunction = $dataArray['function'];
}
$strArgs = "";
if (isset($dataArray['args'])) {
$strArgs = $dataArray['args'];
}
$stack .= "{$strClass}{$strType}{$strFunction}({$strArgs})";
$stack .= "\n";
}
return $stack;
}

/**
Expand Down
Loading