-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve logging structure (#1583)
## What kind of change does this PR introduce? * Remove unformatted logs which do not confirm to JSON * Previously, we were logging both `time` (not UTC) and `timestamp` (in UTC) which is redundant. I've opted to remove `timestamp` and just log the UTC time as the `time` field, which is supported by logrus * Previously, the `request_id` was not being logged because it was unable to retrieve the context properly. Now, the `request_id` field is added to every log entry, which allows us to filter by `request_id` to see the entire lifecycle of the request * Previously, panics weren't being handled properly and they were just logged as text instead of JSON. The server would return an empty reply, which leads to ugly responses like "Unexpected token < in JSON..." if using fetch in JS. Now, the server returns a proper 500 error response: `{"code":500,"error_code":"unexpected_failure","msg":"Internal Server Error"}` * Added tests for `recoverer` and `NewStructuredLogger` to prevent regression * Remove "request started" log since the `request_id` can be used to keep track of the entire request lifecycle. This cuts down on the noise to signal ratio as well. ## Log format * Panics are now logged like this (note the additional fields like `panic` and `stack` - which is a dump of the stack trace): ```json { "component":"api", "duration":6065700500, "level":"info", "method":"GET", "msg":"request completed", "panic":"test panic", "path":"/panic", "referer":"http://localhost:3001", "remote_addr":"127.0.0.1", "request_id":"4cde5f20-2c3c-4645-bc75-52d6231e22e2", "stack":"goroutine 82 [running]:...rest of stack trace omitted for brevity", "status":500, "time":"2024-05-15T09:37:42Z" } ``` * Requests that call `NewAuditLogEntry` will be logged with the `auth_event` payload in this format (note that the timestamp field no longer exists) ```json { "auth_event": { "action": "token_refreshed", "actor_id": "733fb34d-a6f2-43e1-976a-8e6a456b6889", "actor_name": "Kang Ming Tay", "actor_username": "kang.ming1996@gmail.com", "actor_via_sso": false, "log_type": "token" }, "component": "api", "duration": 75945042, "level": "info", "method": "POST", "msg": "request completed", "path": "/token", "referer": "http://localhost:3001", "remote_addr": "127.0.0.1", "request_id": "08c7e47b-42f4-44dc-a39b-7275ef5bbb45", "status": 200, "time": "2024-05-15T09:40:09Z" } ```
- Loading branch information
1 parent
e5f98cb
commit c22fc15
Showing
18 changed files
with
260 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.