stability fixes, lower updates

This commit is contained in:
Dobromir Popov
2025-07-26 22:32:45 +03:00
parent 9576c52039
commit 7c61c12b70
9 changed files with 1210 additions and 45 deletions

View File

@ -55,7 +55,7 @@ class SafeStreamHandler(logging.StreamHandler):
pass
def setup_safe_logging(log_level=logging.INFO, log_file='logs/safe_logging.log'):
"""Setup logging with SafeFormatter and UTF-8 encoding
"""Setup logging with SafeFormatter and UTF-8 encoding with enhanced persistence
Args:
log_level: Logging level (default: INFO)
@ -80,17 +80,42 @@ def setup_safe_logging(log_level=logging.INFO, log_file='logs/safe_logging.log')
))
handlers.append(console_handler)
# File handler with UTF-8 encoding and error handling
# File handler with UTF-8 encoding and error handling - ENHANCED for persistence
try:
encoding_kwargs = {
"encoding": "utf-8",
"errors": "ignore" if platform.system() == "Windows" else "backslashreplace"
}
file_handler = logging.FileHandler(log_file, **encoding_kwargs)
# Use rotating file handler to prevent huge log files
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler(
log_file,
maxBytes=10*1024*1024, # 10MB max file size
backupCount=5, # Keep 5 backup files
**encoding_kwargs
)
file_handler.setFormatter(SafeFormatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
# Force immediate flush for critical logs
class FlushingHandler(RotatingFileHandler):
def emit(self, record):
super().emit(record)
self.flush() # Force flush after each log
# Replace with flushing handler for critical systems
file_handler = FlushingHandler(
log_file,
maxBytes=10*1024*1024,
backupCount=5,
**encoding_kwargs
)
file_handler.setFormatter(SafeFormatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
handlers.append(file_handler)
except (OSError, IOError) as e:
# If file handler fails, just use console handler
@ -109,4 +134,34 @@ def setup_safe_logging(log_level=logging.INFO, log_file='logs/safe_logging.log')
logger = logging.getLogger(logger_name)
for handler in logger.handlers:
handler.setFormatter(safe_formatter)
# Set up signal handlers for graceful shutdown and log flushing
import signal
import atexit
def flush_all_logs():
"""Flush all log handlers"""
for handler in logging.getLogger().handlers:
if hasattr(handler, 'flush'):
handler.flush()
# Force logging shutdown
logging.shutdown()
def signal_handler(signum, frame):
"""Handle shutdown signals"""
print(f"Received signal {signum}, flushing logs...")
flush_all_logs()
sys.exit(0)
# Register signal handlers (Windows compatible)
if platform.system() == "Windows":
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
else:
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)
# Register atexit handler for normal shutdown
atexit.register(flush_all_logs)