This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import contextvars | |
| import uuid | |
| import re | |
| # 1. For Request-Scoped Context (via contextvars) | |
| request_id_var = contextvars.ContextVar("request_id", default="GLOBAL_REQ_N/A") | |
| user_id_var = contextvars.ContextVar("user_id", default="anonymous_cv") | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| class ContextualLogAdapter(logging.LoggerAdapter): | |
| """ | |
| A LoggerAdapter that correctly merges its own 'extra' context | |
| with any 'extra' context provided at call time. | |
| Call-time 'extra' takes precedence. | |
| """ | |
| def process(self, msg, kwargs): | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(component_id)s - %(user_role)s - %(message)s',force=True ) | |
| base_logger = logging.getLogger(__name__) | |
| adapter_context = {'component_id': 'AuthModule', 'user_role':'guest'} | |
| standard_adapter = logging.LoggerAdapter(base_logger, adapter_context) | |
| # extra during the info call will be overriden by `adapter_context` | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| class ContextualLogAdapter(logging.LoggerAdapter): | |
| """ | |
| A LoggerAdapter that correctly merges its own 'extra' context | |
| with any 'extra' context provided at call time. | |
| Call-time 'extra' takes precedence. | |
| """ | |
| def process(self, msg, kwargs): | |
| # Start with the adapter's pre-defined context | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import sys | |
| import re | |
| # 1. Setup root logger and a stream handler | |
| root_logger = logging.getLogger() | |
| root_logger.setLevel(logging.DEBUG) | |
| # Remove any existing handlers from the root logger | |
| # This prevents duplicate logging if the environment has pre-configured handlers. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import contextvars | |
| # 1. Define a ContextVar for the current SQL request ID | |
| request_id_var = contextvars.ContextVar('request_id', default='unknown') | |
| # 2. Configure the core.worker.sql logger | |
| handler = logging.StreamHandler() | |
| handler.setFormatter(logging.Formatter( | |
| '%(asctime)s %(levelname)s [request_id=%(request_id)s] %(message)s' | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import sys | |
| # Configure a simple logger and handler for demonstration | |
| engine_sql_logger = logging.getLogger('core.engine.sql') | |
| engine_sql_logger.setLevel(logging.DEBUG) | |
| console_handler = logging.StreamHandler(sys.stdout) | |
| console_handler.setLevel(logging.DEBUG) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import sys | |
| # 1. Get the root logger and set its level to DEBUG to allow all messages to propagate | |
| root_logger = logging.getLogger() | |
| root_logger.setLevel(logging.DEBUG) | |
| # Clear any default handlers from basicConfig if it was called previously, | |
| # to ensure our custom handlers are the only ones. | |
| if root_logger.handlers: | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| # Configure the root logger to show all messages for demonstration | |
| # In a real app, you'd configure handlers and formatters here too. | |
| logging.basicConfig(level=logging.DEBUG) | |
| # Log a message directly to the root logger using the module-level functions | |
| logging.info("This message goes directly to the root logger.") | |
| # Get a logger for the 'core' namespace | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| # Get the main 'core' logger | |
| core_logger = logging.getLogger('core') | |
| core_logger.setLevel(logging.DEBUG) # Set a low level to capture all messages | |
| # Get child loggers | |
| worker_sql_logger = logging.getLogger('core.worker.sql') | |
| engine_sql_logger = logging.getLogger('core.engine.sql') |