Skip to content

Commit

Permalink
Ignore JSON objects in message value in Monolog default format
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-jozwikowski committed Feb 11, 2024
1 parent 31176f0 commit 7f2e3f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
18 changes: 12 additions & 6 deletions mcv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const result_line_3__unparsed = "2023-10-23 11:03:16: [9a4e77e9afa8] [ERROR] [Wh
const result_line_4__file_change = "==> some/path/to\\file.log <=="
const result_line_5__user_logged = "INFO:default\t2023-10-23 11:07:47\tUser logged in\r\n{\"user\":{\"id\":\"54767261-98c6-4a57-9064-0d35fd06d1fc\"}}"
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 = "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_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__empty = ""

func TestDefault(t *testing.T) {
Expand All @@ -38,7 +39,8 @@ func TestDefault(t *testing.T) {
result_line__empty,
result_line_5__user_logged + "\r\n\n",
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
})
}

Expand All @@ -57,7 +59,8 @@ func TestNoNewLine(t *testing.T) {
result_line__empty,
result_line_5__user_logged + "\r\n",
result_line_6__route_matched + "\r\n",
result_line_7__doctrine + "\r\n",
result_line_7__doctrine_exception + "\r\n",
result_line_8__doctrine_query + "\r\n",
})
}

Expand All @@ -76,7 +79,8 @@ func TestShowFileChange(t *testing.T) {
result_line_4__file_change + "\n\n",
result_line_5__user_logged + "\r\n\n",
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
})
}

Expand All @@ -95,7 +99,8 @@ func TestParsedLinesOnly(t *testing.T) {
result_line__empty,
result_line_5__user_logged + "\r\n\n",
result_line_6__route_matched + "\r\n\n",
result_line_7__doctrine + "\r\n\n",
result_line_7__doctrine_exception + "\r\n\n",
result_line_8__doctrine_query + "\r\n\n",
})
}

Expand All @@ -114,7 +119,8 @@ func TestNoNewLineFileChangeParsedOnly(t *testing.T) {
result_line_4__file_change + "\n",
result_line_5__user_logged + "\r\n",
result_line_6__route_matched + "\r\n",
result_line_7__doctrine + "\r\n",
result_line_7__doctrine_exception + "\r\n",
result_line_8__doctrine_query + "\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 @@ -9,5 +9,6 @@ func GetTestData() []string {
`[2023-10-23T11:07:47.038324+00:00] default.INFO: User logged in {"user":{"id":"54767261-98c6-4a57-9064-0d35fd06d1fc"}} []`,
`[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}} []`,
}
}
13 changes: 11 additions & 2 deletions src/viewer/reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package viewer

import (
"errors"
"monolog-cli-viewer/src/viewer/reader"
"regexp"
"strings"
Expand All @@ -15,8 +16,16 @@ func init() {
}

func InitLogLine(rawLine string) *LogLine {
j, err := objx.FromJSON(rawLine)
if err != nil {
var j objx.Map
var err error
rawLine = strings.Trim(rawLine, "\r\n\t ")
if rawLine[0] == '{' { // if it starts with a JSON opening
j, err = objx.FromJSON(rawLine) // lets try parsing it as JSON
} else {
err = errors.New("not json")
}

if err != nil { // JSON did not work or couldn't work
j, err = reader.MonologFormat(rawLine)
if err != nil {
// cannot have it as JSON so lets at least return the raw line back
Expand Down
4 changes: 4 additions & 0 deletions src/viewer/reader/monolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func extractJSONSegments(input string) ([]string, string) {
// prepend the JSON to the result. We're reading backwards.
result = append([]string{input[index : closingIndex+1]}, result...)
input = input[0:index]

if len(result) == 2 { // we got context, and extra - enough
break
}
}
}
}
Expand Down

0 comments on commit 7f2e3f8

Please sign in to comment.