Skip to content

Commit 1f65007

Browse files
authored
Merge pull request #52 from opcodesio/improvement/handler-more-log-formats-by-default
adjust the Regex expressions to be more forgiving to custom log formats
2 parents 57b6bda + 56a5482 commit 1f65007

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opcodesio/log-viewer",
3-
"version": "v1.2.4",
3+
"version": "v1.2.5",
44
"description": "Fast and easy-to-use log viewer for your Laravel application",
55
"keywords": [
66
"arukompas",

src/Log.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

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

@@ -54,17 +54,32 @@ public function __construct(
5454
$firstLineSplit = str_split($firstLine, 1000);
5555
preg_match($pattern, array_shift($firstLineSplit), $matches);
5656

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

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

65-
$firstLineText = $matches[4];
64+
if (! empty($matches[3])) {
65+
// we have a time offset!
66+
// TODO: handle the offset provided here, which is provided as a string like "+03:00" or "-02:30"
67+
}
68+
69+
$this->environment = $matches[5] ?? '';
70+
71+
// There might be something in the middle between the timestamp
72+
// and the environment/level. Let's put that at the beginning of the first line.
73+
$middle = trim(rtrim($matches[4] ?? '', $this->environment.'.'));
74+
75+
$firstLineText = $matches[6];
76+
77+
if (! empty($middle)) {
78+
$firstLineText = $middle.' '.$firstLineText;
79+
}
80+
6681
$this->text = mb_convert_encoding($firstLineText, 'UTF-8', 'UTF-8');
67-
$text = $firstLineText.($matches[5] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;
82+
$text = $firstLineText.($matches[7] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;
6883

6984
if (session()->get('log-viewer:shorter-stack-traces', false)) {
7085
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);

src/LogReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

1414
const DIRECTION_FORWARD = 'forward';
1515

0 commit comments

Comments
 (0)