From 7d7a2699cc9713cb3603553e77e967103a921608 Mon Sep 17 00:00:00 2001 From: TannedCung Date: Wed, 26 Jun 2024 16:40:33 +0700 Subject: [PATCH] Consilidate logger --- cope2n-api/fwd_api/api/ctel_view.py | 1 + cope2n-api/fwd_api/celery_worker/task_warpper.py | 6 +++--- .../middleware/logging_request_response_middleware.py | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cope2n-api/fwd_api/api/ctel_view.py b/cope2n-api/fwd_api/api/ctel_view.py index dacf052..8bdf416 100755 --- a/cope2n-api/fwd_api/api/ctel_view.py +++ b/cope2n-api/fwd_api/api/ctel_view.py @@ -229,6 +229,7 @@ class CtelViewSet(viewsets.ViewSet): "invoice": invoice_file_objs } rq_id = provider_code + "_" + datetime.now().strftime("%Y%m%d%H%M%S") + "_" + uuid.uuid4().hex + logger.info(f"Creating a request: {rq_id}") is_test_request = validated_data.get("is_test_request", False) count = 0 doc_files_with_type = [] diff --git a/cope2n-api/fwd_api/celery_worker/task_warpper.py b/cope2n-api/fwd_api/celery_worker/task_warpper.py index dc560c8..5b893fa 100644 --- a/cope2n-api/fwd_api/celery_worker/task_warpper.py +++ b/cope2n-api/fwd_api/celery_worker/task_warpper.py @@ -7,10 +7,10 @@ class VerboseTask(Task): def on_failure(self, exc, task_id, args, kwargs, einfo): # Task failed. What do you want to do? - logger.error('FAILURE: Task raised an exception: {}'.format(exc)) + logger.error(f'FAILURE: Task: {self.name} - {task_id} | Task raised an exception: {exc}') def on_success(self, retval, task_id, args, kwargs): - logger.info(f"SUCCESS: Task: {task_id} succeeded | retval: {retval} | args: {args} | kwargs: {kwargs}") + logger.info(f"SUCCESS: Task: {self.name} - {task_id} | retval: {retval} | args: {args} | kwargs: {kwargs}") def before_start(self, task_id, args, kwargs): - logger.info(f"BEFORE_START: Task: {task_id} | args: {args} | kwargs: {kwargs}") \ No newline at end of file + logger.info(f"BEFORE_START: Task: {self.name} - {task_id} | args: {args} | kwargs: {kwargs}") \ No newline at end of file diff --git a/cope2n-api/fwd_api/middleware/logging_request_response_middleware.py b/cope2n-api/fwd_api/middleware/logging_request_response_middleware.py index 132ea71..3ec0730 100644 --- a/cope2n-api/fwd_api/middleware/logging_request_response_middleware.py +++ b/cope2n-api/fwd_api/middleware/logging_request_response_middleware.py @@ -20,7 +20,11 @@ class LoggingMiddleware(MiddlewareMixin): def process_response(self, request, response): trace_id = request.META.get('X-Trace-ID', str(uuid.uuid4())) - response_body = response.content.decode("utf-8") if response.content else "" + try: + response_body = response.content.decode("utf-8") if response.content else "" + except Exception as e: + logger.error(e) + response_body = "" logger.info(f"Response: {request.method} {request.path} | Trace ID: {trace_id} | " f"Status: {response.status_code} | Body: {response_body}")