diff --git a/src/Logs/LaravelLog.php b/src/Logs/LaravelLog.php index 8c89dc88..475acab8 100644 --- a/src/Logs/LaravelLog.php +++ b/src/Logs/LaravelLog.php @@ -106,19 +106,17 @@ protected static function regexPattern(): string protected function extractContextsFromFullText(): void { - // The regex pattern to find JSON strings. - $pattern = '/(\{(?:[^{}]|(?R))*\}|\[(?:[^\[\]]|(?R))*\])/'; $contexts = []; // Find matches. - preg_match_all($pattern, $this->text, $matches); + $json_strings = $this->getJsonStringsFromFullText(); - if (! isset($matches[0])) { + if (empty($json_strings)) { return; } // Loop through the matches. - foreach ($matches[0] as $json_string) { + foreach ($json_strings as $json_string) { // Try to decode the JSON string. If it fails, json_last_error() will return a non-zero value. $json_data = json_decode(trim($json_string), true); @@ -181,4 +179,28 @@ protected function extractMailPreview(): void 'size_formatted' => Utils::bytesForHumans($message->getSize()), ]; } + + protected function getJsonStringsFromFullText(): array + { + $json = ''; + $json_strings = []; + $in = 0; + foreach (str_split($this->text) as $char) { + if ($char == '{' || $char == '[') { + $in++; + } + if ($in) { + $json .= $char; + } + if ($char == '}' || $char == ']') { + $in--; + if ($in == 0) { + $json_strings[] = $json; + $json = ''; + } + } + } + + return $json_strings; + } }