🔍 Code Extractor

function setup_logging_v4

Maturity: 42

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

File:
/tf/active/vicechatdev/contract_validity_analyzer/test_implementation.py
Lines:
23 - 32
Complexity:
simple

Purpose

This function initializes the logging configuration for test environments by setting up dual output handlers (console and file), establishing a consistent log format with timestamps, and setting the logging level to INFO. It creates a 'test_run.log' file to persist test execution logs while simultaneously displaying them in the console.

Source Code

def setup_logging():
    """Setup logging for testing."""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(),
            logging.FileHandler('test_run.log')
        ]
    )

Return Value

This function returns None. It performs a side effect by configuring the global logging system through logging.basicConfig().

Dependencies

  • logging

Required Imports

import logging

Usage Example

import logging

def setup_logging():
    """Setup logging for testing."""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(),
            logging.FileHandler('test_run.log')
        ]
    )

# Call the function to configure logging
setup_logging()

# Now use logging in your tests
logger = logging.getLogger(__name__)
logger.info('Test started')
logger.warning('This is a warning')
logger.error('This is an error')

Best Practices

  • Call this function once at the beginning of your test suite, not before each individual test
  • Be aware that logging.basicConfig() only works if the root logger has not been configured yet; subsequent calls have no effect
  • The 'test_run.log' file will be created in the current working directory; ensure proper cleanup after tests if needed
  • Consider using a unique log filename or timestamp if running multiple test sessions to avoid log file conflicts
  • The INFO level captures informational messages and above (WARNING, ERROR, CRITICAL); adjust level if more verbose DEBUG logging is needed
  • Both handlers will receive all log messages at INFO level and above; consider separate levels for console vs file if needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_test_logging 94.5% 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 91.3% 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
  • function setup_test_logging_v4 90.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
  • function setup_test_logging_v3 88.8% 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_v2 87.1% similar

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

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