Skip to content

Commit

Permalink
Recognize JSON closing tags inside strings to ignore them
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-jozwikowski committed Feb 12, 2024
1 parent 7f2e3f8 commit 2e21c2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions mcv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const result_line_5__user_logged = "INFO:default\t2023-10-23 11:07:47\tUser logg
const result_line_6__route_matched = "INFO:request\t2023-12-29 10:26:40\tMatched route \"api_login\".\r\n{\"method\":\"POST\",\"request_uri\":\"http://localhost/api/v1/login\",\"route\":\"api_login\",\"route_parameters\":{\"_controller\":\"App\\\\User\\\\Infrastructure\\\\Controller\\\\ApiLoginController::index\",\"_route\":\"api_login\"}}"
const result_line_7__doctrine_exception = "ERROR:request\t2023-12-31 11:18:53\tUncaught PHP Exception Symfony\\Component\\HttpKernel\\Exception\\HttpException: \"This value should be of type unknown. This value should not be blank. This value should not be blank.\" at RequestPayloadValueResolver.php line 127\r\n{\"exception\":\"[object] (Symfony\\\\Component\\\\HttpKernel\\\\Exception\\\\HttpException(code: 0): This value should be of type unknown.\\nThis value should not be blank.\\nThis value should not be blank. at /var/www/vendor/symfony/http-kernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php:127)\\n[previous exception] [object] (Symfony\\\\Component\\\\Validator\\\\Exception\\\\ValidationFailedException(code: 0): :\\n This value should be of type unknown.\\nObject(App\\\\Scooter\\\\Infrastructure\\\\Request\\\\UpdateLocation\\\\UpdateLocationRequest).latitude:\\n This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)\\nObject(App\\\\Scooter\\\\Infrastructure\\\\Request\\\\UpdateLocation\\\\UpdateLocationRequest).longitude:\\n This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)\\n at /var/www/vendor/symfony/http-kernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php:127)\"}"
const result_line_8__doctrine_query = "DEBUG:doctrine\t2023-12-29 11:35:33\tExecuting statement: SELECT t0.id AS id_1, t0.email AS email_2, t0.roles AS roles_3, t0.password AS password_4 FROM user_entity t0 WHERE t0.id = ? (parameters: array{\"1\":1}, types: array{\"1\":1})\r\n{\"params\":{\"1\":1},\"sql\":\"SELECT t0.id AS id_1, t0.email AS email_2, t0.roles AS roles_3, t0.password AS password_4 FROM user_entity t0 WHERE t0.id = ?\",\"types\":{\"1\":1}}"
const result_line_9__json_chars_test = "EMERGENCY:app\t2024-02-12 20:29:00\tSome []{} test\r\n{\"invalids\":\"[]{} \\\" \\\\} \"}"
const result_line__empty = ""

func TestDefault(t *testing.T) {
Expand All @@ -41,6 +42,7 @@ func TestDefault(t *testing.T) {
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
result_line_9__json_chars_test + "\r\n\n",
})
}

Expand All @@ -61,6 +63,7 @@ func TestNoNewLine(t *testing.T) {
result_line_6__route_matched + "\r\n",
result_line_7__doctrine_exception + "\r\n",
result_line_8__doctrine_query + "\r\n",
result_line_9__json_chars_test + "\r\n",
})
}

Expand All @@ -81,6 +84,7 @@ func TestShowFileChange(t *testing.T) {
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
result_line_9__json_chars_test + "\r\n\n",
})
}

Expand All @@ -101,6 +105,7 @@ func TestParsedLinesOnly(t *testing.T) {
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
result_line_9__json_chars_test + "\r\n\n",
})
}

Expand All @@ -121,6 +126,7 @@ func TestNoNewLineFileChangeParsedOnly(t *testing.T) {
result_line_6__route_matched + "\r\n",
result_line_7__doctrine_exception + "\r\n",
result_line_8__doctrine_query + "\r\n",
result_line_9__json_chars_test + "\r\n",
})
}

Expand Down
1 change: 1 addition & 0 deletions src/data/test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ func GetTestData() []string {
`[2023-12-29T10:26:40.537772+00:00] request.INFO: Matched route "api_login". {"route":"api_login","route_parameters":{"_route":"api_login","_controller":"App\\User\\Infrastructure\\Controller\\ApiLoginController::index"},"request_uri":"http://localhost/api/v1/login","method":"POST"} []`,
`[2023-12-31T11:18:53.286291+00:00] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\HttpException: "This value should be of type unknown. This value should not be blank. This value should not be blank." at RequestPayloadValueResolver.php line 127 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\HttpException(code: 0): This value should be of type unknown.\nThis value should not be blank.\nThis value should not be blank. at /var/www/vendor/symfony/http-kernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php:127)\n[previous exception] [object] (Symfony\\Component\\Validator\\Exception\\ValidationFailedException(code: 0): :\n This value should be of type unknown.\nObject(App\\Scooter\\Infrastructure\\Request\\UpdateLocation\\UpdateLocationRequest).latitude:\n This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)\nObject(App\\Scooter\\Infrastructure\\Request\\UpdateLocation\\UpdateLocationRequest).longitude:\n This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)\n at /var/www/vendor/symfony/http-kernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php:127)"} []`,
`[2023-12-29T11:35:33.022792+00:00] doctrine.DEBUG: Executing statement: SELECT t0.id AS id_1, t0.email AS email_2, t0.roles AS roles_3, t0.password AS password_4 FROM user_entity t0 WHERE t0.id = ? (parameters: array{"1":1}, types: array{"1":1}) {"sql":"SELECT t0.id AS id_1, t0.email AS email_2, t0.roles AS roles_3, t0.password AS password_4 FROM user_entity t0 WHERE t0.id = ?","params":{"1":1},"types":{"1":1}} []`,
`[2024-02-12T20:29:00.590248+00:00] app.EMERGENCY: Some []{} test {"invalids":"[]{} \" \\} "} []`,
}
}
12 changes: 9 additions & 3 deletions src/viewer/reader/monolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,24 @@ func extractJSONSegments(input string) ([]string, string) {
inputLength := len(input)
openenedCount := 0
closingIndex := inputLength
isStringContext := false
for index := inputLength - 1; index > 0; index-- {
currentChar := input[index]
if currentChar == '}' || currentChar == ']' { // we're at the closing char

if currentChar == '"' && input[index-1] != '\\' { // if the character is a " and not an escaped one
isStringContext = !isStringContext
}

if !isStringContext && (currentChar == '}' || currentChar == ']') { // we're at the closing char and not inside a string
if openenedCount == 0 { // we're not inside JSON
closingIndex = index // means we've found the outermost closing tag
}
openenedCount++
}

if currentChar == '{' || currentChar == '[' { // we're at the opening char
if !isStringContext && (currentChar == '{' || currentChar == '[') { // we're at the opening char and not inside a string
openenedCount--
if openenedCount == 0 {
if openenedCount == 0 { // we just closed the last context
// prepend the JSON to the result. We're reading backwards.
result = append([]string{input[index : closingIndex+1]}, result...)
input = input[0:index]
Expand Down

0 comments on commit 2e21c2d

Please sign in to comment.