본문 바로가기
소프트웨어 마에스트로/BackEnd(Django)

[백엔드] logging 및 api 관련 기록

by alpakaka 2024. 8. 5.

logging 에서 일단 받도록은 만들어놨었다. 이제 프론트가 어느정도 가동이 가능해서 프론트로 보내고 로깅을 찍어봤다.

더보기
todos.logging_middleware ERROR 2024-08-05 11:34:03,245 logging_middleware 57976 6124974080 Request : /auth/api/token/verify/, POST, 401, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:18,328 logging_middleware 57976 6124974080 Request : /auth/login/google/, POST, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:18,541 logging_middleware 57976 6124974080 Request : /auth/user/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:18,774 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:18,800 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:23,351 logging_middleware 57976 6124974080 Request : /todos/todo/, PATCH, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:23,461 logging_middleware 57976 6141800448 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:23,496 logging_middleware 57976 6124974080 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:24,044 logging_middleware 57976 6124974080 Request : /todos/todo/, PATCH, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:24,181 logging_middleware 57976 6141800448 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:24,277 logging_middleware 57976 6124974080 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:41,363 logging_middleware 57976 6124974080 Request : /todos/todo/, POST, 201, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:41,627 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:41,644 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:49,545 logging_middleware 57976 6124974080 Request : /todos/sub/, POST, 201, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:49,674 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:34:49,716 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:35:07,892 logging_middleware 57976 6124974080 Request : /todos/sub/, PATCH, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:35:08,057 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:35:08,132 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:26,773 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:29,387 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:30,215 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:30,437 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:35,651 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware ERROR 2024-08-05 11:35:36,710 logging_middleware 57976 6124974080 Request : /todos/category/, POST, 400, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:36:08,258 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:36:08,284 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:37:08,482 logging_middleware 57976 6124974080 Request : /todos/category/, GET, 200, user_id : None, id : None
todos.logging_middleware INFO 2024-08-05 11:37:08,497 logging_middleware 57976 6141800448 Request : /todos/todo/, GET, 200, user_id : None, id : None

사실 스슥 진행하다가 에러가 났었는데, 이 로깅의 경우에 어디서 왜 오류가 났는지 하나도 확인할 수 없었던 게 문제였다!

로깅을 만들었지만 프론트에서 왜 에러가 발생했는지 확인해야하는 문제..

일단 request body 를 보내도록 해보려고 했으나 뭔가 막혀있었다.

아래와 같이 에러가 발생했다.

django.http.request.RawPostDataException: You cannot access body after reading from request's data stream

 

그래서 일단 기본적으로 에러가 발생했다면 에러의 메세지를 줄 것

그리고 못해도 유저의 아이디를 확인해서 어떤 유저가 해당 api 를 호출했는지 확인 가능할 것

그리고 어떤 파라미터나 body 를 보냈는지 확인하도록 로깅을 작성하고 싶은데 어떻게 해야할지 모르겠다..

 

생각해보니 지금 middleware에서 response 쪽을 받을 수 있기 때문에 이쪽으로 출력하도록 수정했다.

todos.logging_middleware ERROR 2024-08-05 12:04:42,825 logging_middleware 59503 6136983552 Request : /todos/category/, POST, 400, {'error': 'Invalid order'}

 

todos.logging_middleware INFO 2024-08-05 12:04:38,018 logging_middleware 59503 6153809920 Request : /todos/todo/, GET, 200, [{'id': 2, 'content': 'updated', 'category_id': 1, 'start_date': '2024-07-29', 'end_date': '2024-07-29', 'user_id': 000, 'order': '0|hzzzzz:', 'is_completed': False, 'children': []}, {'id': 4, 'content': 'OneTwoThree', 'category_id': 1, 'start_date': None, 'end_date': '2025-04-01', 'user_id': 1, 'order': '0|i0000f:', 'is_completed': False, 'children': []}, {'id': 5, 'content': 'HaveDate', 'category_id': 1, 'start_date': None, 'end_date': '2025-04-01', 'user_id': 1, 'order': '0|i0000h:', 'is_completed': False, 'children': []}, {'id': 6, 'content': 'HaveDateAndHaveSub', 'category_id': 1, 'start_date': None, 'end_date': '2025-04-01', 'user_id': 1, 'order': '0|i0000i:', 'is_completed': False, 'children': [{'id': 2, 'content': 'DontHaveDate', 'todo': 6, 'date': '2024-08-01', 'order': '0|abaaaa:', 'is_completed': False, 'created_at': '2024-07-30T10:40:16.755398Z', 'updated_at': '2024-07-30T10:40:16.755455Z', 'deleted_at': None}]}, {'id': 45, 'content': 'Santa tell me', 'category_id': 1, 'start_date': '2024-08-05', 'end_date': '2024-08-05', 'user_id': 1, 'order': '0|i0000q:', 'is_completed': False, 'children': [{'id': 23, 'content': 'Okay bye~~', 'todo': 45, 'date': '2024-08-05', 'order': '1722857688', 'is_completed': False, 'created_at': '2024-08-05T11:34:49.535391Z', 'updated_at': '2024-08-05T12:01:36.312379Z', 'deleted_at': None}]}]

일단 에러 로깅은 잘 나오니 일단 진행한다.

우리 팀의 로깅의 목적은 아직까진 에러가 발생했을 때 대응이 가능하도록 하는 용이기 때문에..

일단 많은 데이터를 보여주고 나중에 하나씩 줄여나가는 식으로 하는편이 나을 것 같다.