Merge pull request #153 from SDSRV-IDP/BE/logging
Add: TraceID for every logging
This commit is contained in:
commit
3c5ea4b7f0
@ -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': '{',
|
||||
},
|
||||
},
|
||||
|
@ -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,8 +128,10 @@ 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)
|
||||
|
||||
c_connector = CeleryConnector()
|
||||
c_connector = CeleryConnector()
|
9
cope2n-api/fwd_api/middleware/local_storage.py
Normal file
9
cope2n-api/fwd_api/middleware/local_storage.py
Normal 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
|
@ -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", "")
|
||||
@ -35,4 +37,11 @@ class LoggingMiddleware(MiddlewareMixin):
|
||||
def process_exception(self, request, exception):
|
||||
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}")
|
||||
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
|
Loading…
Reference in New Issue
Block a user