🔍 Code Extractor

function setup_logging_v2

Maturity: 48

Configures Python's logging system with console and rotating file handlers, creating necessary directories and setting appropriate log levels for the application and third-party libraries.

File:
/tf/active/vicechatdev/email-forwarder/src/utils/logger.py
Lines:
17 - 58
Complexity:
moderate

Purpose

This function initializes a comprehensive logging configuration for an application. It creates a logs directory if needed, sets up both console and file-based logging with rotation (10MB max size, 5 backups), applies custom log levels from settings, and suppresses verbose output from specific third-party libraries (aiosmtpd and msal). This is typically called once at application startup to establish consistent logging behavior throughout the application lifecycle.

Source Code

def setup_logging():
    """Set up logging configuration."""
    
    # Create logs directory if it doesn't exist
    log_dir = Path(settings.LOG_FILE).parent
    log_dir.mkdir(parents=True, exist_ok=True)
    
    # Configure root logger
    root_logger = logging.getLogger()
    root_logger.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
    
    # Remove existing handlers
    for handler in root_logger.handlers[:]:
        root_logger.removeHandler(handler)
    
    # Create formatter
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )
    
    # Console handler
    console_handler = logging.StreamHandler()
    console_handler.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)
    
    # File handler with rotation
    if settings.LOG_FILE:
        file_handler = logging.handlers.RotatingFileHandler(
            settings.LOG_FILE,
            maxBytes=10*1024*1024,  # 10MB
            backupCount=5
        )
        file_handler.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
        file_handler.setFormatter(formatter)
        root_logger.addHandler(file_handler)
    
    # Set specific logger levels
    logging.getLogger('aiosmtpd').setLevel(logging.WARNING)
    logging.getLogger('msal').setLevel(logging.WARNING)
    
    return root_logger

Return Value

Returns the configured root logger instance (logging.Logger object) that can be used for logging throughout the application. This logger has handlers attached for both console output and file rotation, formatted with timestamp, logger name, level, and message.

Dependencies

  • logging
  • pathlib
  • config

Required Imports

import logging
import logging.handlers
from pathlib import Path
from config import settings

Usage Example

# config.py
class Settings:
    LOG_FILE = 'logs/app.log'
    LOG_LEVEL = 'INFO'

settings = Settings()

# main.py
import logging
import logging.handlers
from pathlib import Path
from config import settings

def setup_logging():
    log_dir = Path(settings.LOG_FILE).parent
    log_dir.mkdir(parents=True, exist_ok=True)
    root_logger = logging.getLogger()
    root_logger.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
    for handler in root_logger.handlers[:]:
        root_logger.removeHandler(handler)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    console_handler = logging.StreamHandler()
    console_handler.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)
    if settings.LOG_FILE:
        file_handler = logging.handlers.RotatingFileHandler(settings.LOG_FILE, maxBytes=10*1024*1024, backupCount=5)
        file_handler.setLevel(getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO))
        file_handler.setFormatter(formatter)
        root_logger.addHandler(file_handler)
    logging.getLogger('aiosmtpd').setLevel(logging.WARNING)
    logging.getLogger('msal').setLevel(logging.WARNING)
    return root_logger

# Initialize logging at application startup
logger = setup_logging()
logger.info('Application started')
logger.debug('Debug information')
logger.error('An error occurred')

Best Practices

  • Call this function once at application startup, before any logging operations occur
  • Ensure the settings module is properly configured with LOG_FILE and LOG_LEVEL before calling
  • The function removes existing handlers, so it's safe to call multiple times (e.g., for reconfiguration)
  • File rotation is set to 10MB per file with 5 backups, adjust maxBytes and backupCount if different retention is needed
  • The function suppresses verbose logging from aiosmtpd and msal libraries; add similar lines for other noisy third-party libraries
  • If LOG_FILE is None or empty string, only console logging will be active
  • The parent directory for LOG_FILE is created automatically with parents=True, so nested paths are supported
  • Log level defaults to INFO if an invalid level string is provided in settings
  • Consider calling this before importing other application modules that may use logging

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_logging_v1 82.7% similar

    Configures and initializes Python's logging system with customizable log level and output destination (console or file).

    From: /tf/active/vicechatdev/QA_updater/utils/logging_utils.py
  • function setup_logging 81.1% similar

    Configures and initializes a Python logging system with both console and rotating file handlers, supporting customizable log levels, formats, and file rotation policies.

    From: /tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
  • function setup_logging_v4 77.3% similar

    Configures Python's logging system for testing purposes with both console and file output.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_implementation.py
  • function setup_test_logging 73.4% similar

    Configures Python logging with both console and file output for test execution, returning a logger instance for the calling module.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_local_document.py
  • function setup_test_logging_v1 72.9% similar

    Configures Python logging for test environments with both console and file output handlers.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_single_document.py
← Back to Browse