File size: 3,263 Bytes
e275025 |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
"""
Centralized logging configuration for Medical Transcriber application.
Provides consistent logging across all modules with file and console output.
"""
import logging
import logging.handlers
from pathlib import Path
from datetime import datetime
from typing import Optional
from .constants import LoggingConfig, PROJECT_ROOT, LOGS_DIR
class LoggerSetup:
"""Centralized logger configuration."""
_initialized = False
@classmethod
def setup(cls, log_file: Optional[str] = None, level: str = LoggingConfig.LOG_LEVEL) -> None:
"""
Initialize logging configuration for the entire application.
Args:
log_file: Optional custom log file name. If None, uses auto-generated name.
level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
"""
if cls._initialized:
return
# Create logs directory if it doesn't exist
LOGS_DIR.mkdir(parents=True, exist_ok=True)
# Generate log file path
if log_file is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = f"transcription_{timestamp}.log"
log_path = LOGS_DIR / log_file
# Create root logger
root_logger = logging.getLogger()
root_logger.setLevel(getattr(logging, level))
# File handler
file_handler = logging.handlers.RotatingFileHandler(
log_path,
maxBytes=10 * 1024 * 1024, # 10 MB
backupCount=5,
encoding='utf-8'
)
file_handler.setLevel(getattr(logging, level))
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(getattr(logging, level))
# Formatter
formatter = logging.Formatter(
LoggingConfig.LOG_FORMAT,
datefmt=LoggingConfig.LOG_DATE_FORMAT
)
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add handlers to root logger
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
cls._initialized = True
root_logger.info(f"Logging initialized. Log file: {log_path}")
@classmethod
def get_logger(cls, name: str) -> logging.Logger:
"""
Get logger instance for a module.
Args:
name: Module name (usually __name__)
Returns:
Configured logger instance
"""
if not cls._initialized:
cls.setup()
return logging.getLogger(name)
def configure_logging(
log_file: Optional[str] = None,
level: str = LoggingConfig.LOG_LEVEL
) -> None:
"""
Configure logging for the application.
Args:
log_file: Optional custom log file name
level: Logging level
"""
LoggerSetup.setup(log_file, level)
def get_logger(name: str) -> logging.Logger:
"""
Get a logger instance.
Args:
name: Logger name (usually __name__)
Returns:
Configured logger instance
"""
return LoggerSetup.get_logger(name)
|