Merge pull request #153 from SDSRV-IDP/BE/logging

Add: TraceID for every logging
This commit is contained in:
Le Van Tuan 2024-07-05 16:10:31 +07:00 committed by GitHub Enterprise
commit 3c5ea4b7f0
4 changed files with 32 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import os
from pathlib import Path
import environ
from django.urls import reverse_lazy
from fwd_api.middleware.logging_request_response_middleware import TraceIDLogFilter
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@ -262,13 +263,18 @@ CACHES = {
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'trace_id': {
'()': TraceIDLogFilter,
},
},
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{module}} - {{message}}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{message}}',
'style': '{',
},
},

View File

@ -2,6 +2,7 @@ from celery import Celery
from fwd import settings
from fwd_api.exception.exceptions import GeneralException
from fwd_api.middleware.local_storage import get_current_request
from kombu.utils.uuid import uuid
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
@ -127,7 +128,9 @@ class CeleryConnector:
def send_task(self, name=None, args=None, countdown=None):
if name not in self.task_routes or 'queue' not in self.task_routes[name]:
raise GeneralException("System")
task_id = args[0] + "_" + uuid()[:4] if isinstance(args, tuple) and is_it_an_index(args[0]) else uuid()
# task_id = args[0] + "_" + uuid()[:4] if isinstance(args, tuple) and is_it_an_index(args[0]) else uuid()
request = get_current_request()
task_id = request.META.get('X-Trace-ID', uuid()) + "_" + uuid()[:4] if request else uuid()
logger.info(f"SEND task name: {name} - {task_id} | args: {args} | countdown: {countdown}")
return self.app.send_task(name, args, queue=self.task_routes[name]['queue'], expires=300, countdown=countdown, task_id=task_id)

View File

@ -0,0 +1,9 @@
from threading import local
_thread_locals = local()
def get_current_request():
return getattr(_thread_locals, 'request', None)
def set_current_request(request):
_thread_locals.request = request

View File

@ -2,6 +2,7 @@ import logging
import uuid
from django.utils.deprecation import MiddlewareMixin
from .local_storage import set_current_request, get_current_request
logger = logging.getLogger(__name__)
@ -9,6 +10,7 @@ class LoggingMiddleware(MiddlewareMixin):
def process_request(self, request):
trace_id = request.headers.get('X-Trace-ID', str(uuid.uuid4()))
request.META['X-Trace-ID'] = trace_id
set_current_request(request)
request_body = ""
content_type = request.headers.get("Content-Type", "")
@ -36,3 +38,10 @@ class LoggingMiddleware(MiddlewareMixin):
trace_id = request.META.get('X-Trace-ID', str(uuid.uuid4()))
logger.error(f"Exception: {request.method} {request.path} | Trace ID: {trace_id} | "
f"Error: {exception}")
class TraceIDLogFilter(logging.Filter):
def filter(self, record):
request = get_current_request()
trace_id = request.META.get('X-Trace-ID', 'unknown') if request else 'unknown'
record.trace_id = trace_id
return True