Files
ALwrity/backend/utils/logger_utils.py
ajaysi d557bd4918 Fix merge conflicts and resolve circular import issues
- Resolve conflict markers in logging_config.py, main.py, app.py
- Fix circular imports in story_writer services (image/audio/video generation)
  by using lazy imports for get_story_media_write_dir
- Restore clean versions of:
  - sif_agents.py
  - tenant_provider_config.py
  - personalization_service.py
  - huggingface_provider.py
  - main_text_generation.py
  - logger_utils.py
- Use setup_clean_logging() consistently across app.py and main.py
- Restore verbose_mode handling in start_alwrity_backend.py
2026-03-22 10:45:05 +05:30

54 lines
1.6 KiB
Python

"""
Logger utilities to prevent conflicts between different logging configurations.
"""
from loguru import logger
import sys
def safe_logger_config(format_string: str, level: str = "INFO"):
"""
Safely configure logger without removing existing handlers.
This prevents conflicts with the main logging configuration.
Args:
format_string: Log format string
level: Log level
"""
try:
# Only add a new handler if we don't already have one with this format
existing_handlers = logger._core.handlers
for handler in existing_handlers:
if hasattr(handler, '_sink') and handler._sink == sys.stdout:
# Check if format is similar to avoid duplicates
if hasattr(handler, '_format') and handler._format == format_string:
return # Handler already exists with this format
# Add new handler only if needed
logger.add(
sys.stdout,
level=level,
format=format_string,
colorize=True
)
except Exception as e:
# If there's any error, just use the existing logger configuration
pass
def get_service_logger(service_name: str, format_string: str = None):
"""
Get a logger for a specific service without conflicting with main configuration.
Args:
service_name: Name of the service
format_string: Optional custom format string
Returns:
Logger instance
"""
if format_string:
safe_logger_config(format_string)
return logger.bind(service=service_name)