🔍 Code Extractor

function setup_logging

Maturity: 60

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

File:
/tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
Lines:
12 - 66
Complexity:
moderate

Purpose

This function sets up a comprehensive logging infrastructure for applications, particularly suited for long-running services like contract analyzers. It creates a dual-output logging system (console and file), implements log rotation to prevent disk space issues, and suppresses verbose output from common third-party libraries. The function ensures log directories exist, configures formatters, and returns a ready-to-use logger instance with all handlers properly configured.

Source Code

def setup_logging(config: Dict[str, Any], log_dir: str = "logs") -> logging.Logger:
    """
    Set up logging configuration.
    
    Args:
        config: Logging configuration dictionary
        log_dir: Directory to store log files
        
    Returns:
        Configured logger instance
    """
    # Create log directory if it doesn't exist
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    
    # Get configuration values
    log_level = config.get('level', 'INFO')
    log_format = config.get('format', '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    log_file = config.get('file', 'contract_analyzer.log')
    max_file_size_mb = config.get('max_file_size_mb', 10)
    backup_count = config.get('backup_count', 3)
    
    # Create formatters
    formatter = logging.Formatter(log_format)
    
    # Set up root logger
    root_logger = logging.getLogger()
    root_logger.setLevel(getattr(logging, log_level.upper()))
    
    # Clear existing handlers
    root_logger.handlers.clear()
    
    # Console handler
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(getattr(logging, log_level.upper()))
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)
    
    # File handler with rotation
    log_file_path = os.path.join(log_dir, log_file)
    file_handler = RotatingFileHandler(
        log_file_path,
        maxBytes=max_file_size_mb * 1024 * 1024,
        backupCount=backup_count
    )
    file_handler.setLevel(getattr(logging, log_level.upper()))
    file_handler.setFormatter(formatter)
    root_logger.addHandler(file_handler)
    
    # Set specific logger levels for noisy libraries
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('httpx').setLevel(logging.WARNING)
    
    return root_logger

Parameters

Name Type Default Kind
config Dict[str, Any] - positional_or_keyword
log_dir str 'logs' positional_or_keyword

Parameter Details

config: Dictionary containing logging configuration options. Expected keys: 'level' (str, log level like 'INFO', 'DEBUG', 'WARNING', 'ERROR', 'CRITICAL'), 'format' (str, Python logging format string), 'file' (str, log filename), 'max_file_size_mb' (int, maximum size of each log file in megabytes before rotation), 'backup_count' (int, number of backup log files to keep). All keys are optional with sensible defaults.

log_dir: String path to the directory where log files will be stored. Defaults to 'logs'. The directory will be created if it doesn't exist. Can be relative or absolute path.

Return Value

Type: logging.Logger

Returns a configured logging.Logger instance (specifically the root logger). This logger has both console (stdout) and rotating file handlers attached, with the specified log level and format. The logger is ready for immediate use and will write to both console and file simultaneously. Third-party library loggers (urllib3, requests, httpx) are automatically set to WARNING level to reduce noise.

Dependencies

  • logging
  • os
  • sys

Required Imports

import logging
import os
import sys
from typing import Dict, Any
from logging.handlers import RotatingFileHandler

Usage Example

import logging
import os
import sys
from typing import Dict, Any
from logging.handlers import RotatingFileHandler

def setup_logging(config: Dict[str, Any], log_dir: str = "logs") -> logging.Logger:
    # ... (function code as provided)
    pass

# Basic usage with default settings
config = {
    'level': 'INFO',
    'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    'file': 'app.log',
    'max_file_size_mb': 10,
    'backup_count': 3
}

logger = setup_logging(config, log_dir='./logs')
logger.info('Application started')
logger.debug('Debug information')
logger.error('An error occurred')

# Minimal usage with defaults
minimal_config = {}
logger = setup_logging(minimal_config)
logger.info('Using default configuration')

# Custom log directory and settings
custom_config = {
    'level': 'DEBUG',
    'max_file_size_mb': 50,
    'backup_count': 5,
    'file': 'contract_analyzer.log'
}
logger = setup_logging(custom_config, log_dir='/var/log/myapp')
logger.debug('Detailed debug information')

Best Practices

  • Call this function once at application startup before any logging operations
  • Ensure the log_dir path has appropriate write permissions before calling
  • Use appropriate log levels: DEBUG for development, INFO for production, WARNING/ERROR for critical systems
  • Consider disk space when setting max_file_size_mb and backup_count (total space = max_file_size_mb * (backup_count + 1))
  • The function clears existing handlers on the root logger, so call it only once or be aware of handler duplication
  • Default log file name 'contract_analyzer.log' suggests this was designed for a specific application; customize the 'file' config key for your use case
  • The function automatically suppresses verbose logging from urllib3, requests, and httpx libraries; add more libraries to this list if needed
  • Use getLogger(__name__) in modules to create child loggers that inherit this configuration
  • Log rotation happens automatically when file size exceeds max_file_size_mb, with old logs renamed to .log.1, .log.2, etc.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_logging_v1 81.1% similar

    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.

    From: /tf/active/vicechatdev/email-forwarder/src/utils/logger.py
  • function setup_logging_v3 75.9% 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 74.0% 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_v3 71.2% 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_ocr_retry.py
  • function setup_test_logging_v4 70.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_simulated_document.py
← Back to Browse