Spaces:
Runtime error
Runtime error
File size: 2,311 Bytes
0c527d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import logging
#from main import trace_id_var # Import the context variable from your main file
import main
# 1. Create a custom filter to inject the traceId
class TraceIdFilter(logging.Filter):
"""
A logging filter that injects the traceId from a context variable
into the log record.
"""
def filter(self, record):
# Get the traceId from the context variable and add it to the log record
record.traceId = main.trace_id_var.get()
return True
# 2. Define the dictionary-based logging configuration
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
# The magic happens here: we use the custom attribute '%(traceId)s'
"format": "%(asctime)s [%(traceId)s] %(levelname)-8s [%(threadName)s] %(name)s:%(lineno)d - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
},
# Add a new formatter specifically for colored console output
"colored": {
# Use the special formatter class from colorlog
"()": "colorlog.ColoredFormatter",
# Add color codes to the format string
"format": "%(asctime)s [%(traceId)s] %(log_color)s%(levelname)-8s%(reset)s [%(threadName)s] %(name)s:%(lineno)d - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"filters": {
# Register our custom filter
"trace_id_filter": {
"()": TraceIdFilter,
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "colored",
"filters": ["trace_id_filter"], # Attach the filter to the handler
"stream": "ext://sys.stdout",
},
},
"root": {
"level": "INFO",
"handlers": ["console"],
},
"loggers": {
# Configure uvicorn loggers to use our format
"uvicorn": {"handlers": ["console"], "level": "INFO", "propagate": False},
"uvicorn.error": {"level": "INFO"},
"uvicorn.access": {"handlers": ["console"], "level": "INFO", "propagate": False},
"hypercorn": {"handlers": ["console"], "level": "INFO", "propagate": False},
"hypercorn.access": {"handlers": ["console"], "level": "INFO", "propagate": False}
}
}
|