11import logging
2+ import uuid
3+ from contextvars import ContextVar
24from datetime import datetime
35from logging .config import dictConfig
46
57from pythonjsonlogger import jsonlogger
68
7- from seedwork .infrastructure .request_context import request_context
89from seedwork .utils .functional import SimpleLazyObject
910
11+ correlation_id : ContextVar [uuid .UUID ] = ContextVar (
12+ "correlation_id" , default = uuid .UUID ("00000000-0000-0000-0000-000000000000" )
13+ )
14+
1015
1116class RequestContextFilter (logging .Filter ):
1217 """ "Provides correlation id parameter for the logger"""
1318
14- def __init__ (self , name : str , request_context ) -> None :
19+ def __init__ (self , name : str , correlation_id ) -> None :
1520 super ().__init__ (name = name )
16- self .request_context = request_context
21+ self .correlation_id = correlation_id
1722
1823 def filter (self , record ):
19- record .correlation_id = self .request_context . correlation_id .get ()
24+ record .correlation_id = self .correlation_id .get ()
2025 return True
2126
2227
@@ -40,11 +45,11 @@ def configure(
4045 cls ,
4146 logger_name = "app" ,
4247 log_filename = "./logs.json" ,
43- request_context = request_context ,
48+ correlation_id = correlation_id ,
4449 ):
4550 cls .logger_name = logger_name
4651 cls .log_filename = log_filename
47- cls .request_context = request_context
52+ cls .correlation_id = correlation_id
4853 cls ._configured = True
4954
5055 @classmethod
@@ -131,9 +136,10 @@ def create_logger(cls):
131136
132137 dictConfig (logging_config )
133138 logger = logging .getLogger (name = cls .logger_name )
139+ logger .correlation_id = cls .correlation_id
134140 logger .addFilter (
135141 RequestContextFilter (
136- name = cls .logger_name , request_context = cls .request_context
142+ name = cls .logger_name , correlation_id = cls .correlation_id
137143 )
138144 )
139145 return logger
0 commit comments