Skip to content

Commit

Permalink
Merge pull request #52 from opcodesio/improvement/handler-more-log-fo…
Browse files Browse the repository at this point in the history
…rmats-by-default

adjust the Regex expressions to be more forgiving to custom log formats
  • Loading branch information
arukompas authored Sep 1, 2022
2 parents 57b6bda + 56a5482 commit 1f65007
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opcodesio/log-viewer",
"version": "v1.2.4",
"version": "v1.2.5",
"description": "Fast and easy-to-use log viewer for your Laravel application",
"keywords": [
"arukompas",
Expand Down
23 changes: 19 additions & 4 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Log
{
const LOG_CONTENT_PATTERN = '/^\[(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{6}[\+-]\d\d:\d\d)?)\](?:.*?(\w+)\.|.*?)';
const LOG_CONTENT_PATTERN = '/^\[(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.?(\d{6}([\+-]\d\d:\d\d)?)?)\](.*?(\w+)\.|.*?)';

const LOG_CONTENT_PATTERN_2 = ': (.*?)( in [\/].*?:[0-9]+)?$/is';

Expand Down Expand Up @@ -54,17 +54,32 @@ public function __construct(
$firstLineSplit = str_split($firstLine, 1000);
preg_match($pattern, array_shift($firstLineSplit), $matches);

$this->environment = $matches[3] ?? '';
$this->time = Carbon::parse($matches[1])->tz(config('app.timezone', 'UTC'));

if (! empty($matches[2])) {
// we got microseconds!
$this->time = $this->time->micros((int) $matches[2]);
}

$firstLineText = $matches[4];
if (! empty($matches[3])) {
// we have a time offset!
// TODO: handle the offset provided here, which is provided as a string like "+03:00" or "-02:30"
}

$this->environment = $matches[5] ?? '';

// There might be something in the middle between the timestamp
// and the environment/level. Let's put that at the beginning of the first line.
$middle = trim(rtrim($matches[4] ?? '', $this->environment.'.'));

$firstLineText = $matches[6];

if (! empty($middle)) {
$firstLineText = $middle.' '.$firstLineText;
}

$this->text = mb_convert_encoding($firstLineText, 'UTF-8', 'UTF-8');
$text = $firstLineText.($matches[5] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;
$text = $firstLineText.($matches[7] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;

if (session()->get('log-viewer:shorter-stack-traces', false)) {
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);
Expand Down
2 changes: 1 addition & 1 deletion src/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LogReader
{
const LOG_MATCH_PATTERN = '/\[\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{6}[\+-]\d\d:\d\d)?\].*/';
const LOG_MATCH_PATTERN = '/\[\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d{6}([\+-]\d\d:\d\d)?)?\].*/';

const DIRECTION_FORWARD = 'forward';

Expand Down

0 comments on commit 1f65007

Please sign in to comment.