fix logging issue

This commit is contained in:
Broque Thomas 2025-08-09 13:17:11 -07:00
parent f0957d8167
commit d9e41bc12f
3 changed files with 41 additions and 11 deletions

View file

@ -49,7 +49,7 @@ class PlexScanManager:
Args: Args:
reason: Optional reason for the scan request (for logging) reason: Optional reason for the scan request (for logging)
""" """
logger.info(f"🔍 DEBUG: Plex scan requested - reason: {reason}") logger.info(f"DEBUG: Plex scan requested - reason: {reason}")
with self._lock: with self._lock:
if self._scan_in_progress: if self._scan_in_progress:
# Plex is currently scanning - mark that we need another scan later # Plex is currently scanning - mark that we need another scan later
@ -78,8 +78,8 @@ class PlexScanManager:
with self._lock: with self._lock:
if callback not in self._scan_completion_callbacks: if callback not in self._scan_completion_callbacks:
self._scan_completion_callbacks.append(callback) self._scan_completion_callbacks.append(callback)
logger.info(f"🔍 DEBUG: Added scan completion callback: {callback.__name__}") logger.info(f"DEBUG: Added scan completion callback: {callback.__name__}")
logger.info(f"🔍 DEBUG: Total callbacks registered: {len(self._scan_completion_callbacks)}") logger.info(f"DEBUG: Total callbacks registered: {len(self._scan_completion_callbacks)}")
def remove_scan_completion_callback(self, callback): def remove_scan_completion_callback(self, callback):
""" """
@ -213,11 +213,11 @@ class PlexScanManager:
# Check if Plex is still scanning # Check if Plex is still scanning
is_scanning = self.plex_client.is_library_scanning("Music") is_scanning = self.plex_client.is_library_scanning("Music")
logger.info(f"🔍 DEBUG: Plex scan status check - is_scanning: {is_scanning}") logger.info(f"DEBUG: Plex scan status check - is_scanning: {is_scanning}")
if is_scanning: if is_scanning:
# Still scanning, poll again in 30 seconds # Still scanning, poll again in 30 seconds
logger.info("🔍 DEBUG: Plex library still scanning, will check again in 30 seconds") logger.info("DEBUG: Plex library still scanning, will check again in 30 seconds")
threading.Timer(30, self._poll_scan_status).start() threading.Timer(30, self._poll_scan_status).start()
else: else:
# Scan completed! # Scan completed!
@ -260,12 +260,12 @@ class PlexScanManager:
with self._lock: with self._lock:
callbacks = self._scan_completion_callbacks.copy() # Copy to avoid lock issues callbacks = self._scan_completion_callbacks.copy() # Copy to avoid lock issues
logger.info(f"🔍 DEBUG: Calling {len(callbacks)} scan completion callbacks") logger.info(f"DEBUG: Calling {len(callbacks)} scan completion callbacks")
for callback in callbacks: for callback in callbacks:
try: try:
logger.info(f"🔍 DEBUG: Executing callback: {callback.__name__}") logger.info(f"DEBUG: Executing callback: {callback.__name__}")
callback() callback()
logger.info(f"🔍 DEBUG: Callback {callback.__name__} completed successfully") logger.info(f"DEBUG: Callback {callback.__name__} completed successfully")
except Exception as e: except Exception as e:
logger.error(f"Error in scan completion callback {callback.__name__}: {e}") logger.error(f"Error in scan completion callback {callback.__name__}: {e}")

View file

@ -1,10 +1,37 @@
import logging import logging
import sys import sys
import re
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
class ColoredFormatter(logging.Formatter): class SafeFormatter(logging.Formatter):
"""Formatter that handles Unicode characters safely on Windows"""
@staticmethod
def strip_emojis(text):
"""Remove emoji characters from text for Windows compatibility"""
# Remove emoji characters but keep other Unicode
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
return emoji_pattern.sub('', text)
def format(self, record):
# Try to format with emojis first, fall back to stripped version
try:
return super().format(record)
except UnicodeEncodeError:
# Strip emojis and try again for Windows compatibility
record.getMessage = lambda: self.strip_emojis(record.msg % record.args if record.args else record.msg)
return super().format(record)
class ColoredFormatter(SafeFormatter):
COLORS = { COLORS = {
'DEBUG': '\033[94m', 'DEBUG': '\033[94m',
'INFO': '\033[92m', 'INFO': '\033[92m',
@ -32,6 +59,9 @@ def setup_logging(level: str = "INFO", log_file: Optional[str] = None) -> loggin
console_handler = logging.StreamHandler(sys.stdout) console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(log_level) console_handler.setLevel(log_level)
# Force UTF-8 encoding for Windows compatibility with Unicode characters
if hasattr(console_handler.stream, 'reconfigure'):
console_handler.stream.reconfigure(encoding='utf-8', errors='replace')
console_formatter = ColoredFormatter( console_formatter = ColoredFormatter(
fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s', fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
@ -44,10 +74,10 @@ def setup_logging(level: str = "INFO", log_file: Optional[str] = None) -> loggin
log_path = Path(log_file) log_path = Path(log_file)
log_path.parent.mkdir(parents=True, exist_ok=True) log_path.parent.mkdir(parents=True, exist_ok=True)
file_handler = logging.FileHandler(log_path) file_handler = logging.FileHandler(log_path, encoding='utf-8')
file_handler.setLevel(log_level) file_handler.setLevel(log_level)
file_formatter = logging.Formatter( file_formatter = SafeFormatter(
fmt='%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s', fmt='%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S' datefmt='%Y-%m-%d %H:%M:%S'
) )