""" Safe Logging Wrapper This module provides a logging wrapper that handles Unicode encoding errors gracefully, preventing crashes when logging special characters on Windows consoles. """ import logging import sys import traceback class SafeLogger: """Wrapper for logging.Logger to safely handle unicode characters""" def __init__(self, name): self.logger = logging.getLogger(name) def info(self, msg, *args, **kwargs): try: self.logger.info(msg, *args, **kwargs) except UnicodeEncodeError: # Fallback: sanitize message or print safely safe_msg = msg.encode('ascii', 'replace').decode('ascii') self.logger.info(safe_msg, *args, **kwargs) except Exception: # Last resort fallback pass def warning(self, msg, *args, **kwargs): try: self.logger.warning(msg, *args, **kwargs) except UnicodeEncodeError: safe_msg = msg.encode('ascii', 'replace').decode('ascii') self.logger.warning(safe_msg, *args, **kwargs) except Exception: pass def error(self, msg, *args, **kwargs): try: self.logger.error(msg, *args, **kwargs) except UnicodeEncodeError: safe_msg = msg.encode('ascii', 'replace').decode('ascii') self.logger.error(safe_msg, *args, **kwargs) except Exception: pass def debug(self, msg, *args, **kwargs): try: self.logger.debug(msg, *args, **kwargs) except UnicodeEncodeError: safe_msg = msg.encode('ascii', 'replace').decode('ascii') self.logger.debug(safe_msg, *args, **kwargs) except Exception: pass def exception(self, msg, *args, **kwargs): try: self.logger.exception(msg, *args, **kwargs) except UnicodeEncodeError: safe_msg = msg.encode('ascii', 'replace').decode('ascii') self.logger.exception(safe_msg, *args, **kwargs) except Exception: pass def get_logger(name): return SafeLogger(name)