diff --git a/mcv_test.go b/mcv_test.go index 9e04778..efb66a9 100644 --- a/mcv_test.go +++ b/mcv_test.go @@ -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) { @@ -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", }) } @@ -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", }) } @@ -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", }) } @@ -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", }) } @@ -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", }) } diff --git a/src/data/test_data.go b/src/data/test_data.go index 9c719bd..3cf24d7 100644 --- a/src/data/test_data.go +++ b/src/data/test_data.go @@ -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}} []`, } } diff --git a/src/viewer/reader.go b/src/viewer/reader.go index bbeca5c..0f83bd4 100644 --- a/src/viewer/reader.go +++ b/src/viewer/reader.go @@ -1,6 +1,7 @@ package viewer import ( + "errors" "monolog-cli-viewer/src/viewer/reader" "regexp" "strings" @@ -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 diff --git a/src/viewer/reader/monolog.go b/src/viewer/reader/monolog.go index 9cc89c4..aa65cbc 100644 --- a/src/viewer/reader/monolog.go +++ b/src/viewer/reader/monolog.go @@ -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 + } } } }