Skip to content

Instantly share code, notes, and snippets.

View nadeem4's full-sized avatar
🏠
Working from home

CodeWithNK nadeem4

🏠
Working from home
View GitHub Profile
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")
@nadeem4
nadeem4 / python_contextual_logger_adapter_example.py
Created June 1, 2025 14:57
Example of advanced Python logging featuring a custom `ContextualLogAdapter` for rich, contextual log messages. This script demonstrates: - A `ContextualLogAdapter` that merges adapter-initialized 'extra' data with call-time 'extra' data (call-time takes precedence). - Usage of the adapter within a `DatabaseConnector` class to provide instance-s…
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):
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`
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
@nadeem4
nadeem4 / sensitive_data_filter.py
Last active May 31, 2025 13:38
Python script for setting up logging with a custom filter to redact sensitive data (e.g., passwords) from console output. Demonstrates how to prevent duplicate log messages by clearing existing handlers from the root logger before applying custom configuration.
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.
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'
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)
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:
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
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')