🔍 Code Extractor

function setup_test_logging_v3

Maturity: 42

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

File:
/tf/active/vicechatdev/contract_validity_analyzer/test_ocr_retry.py
Lines:
22 - 32
Complexity:
simple

Purpose

This function initializes the Python logging system specifically for test scenarios. It sets up dual output handlers (console via stdout and file 'test_ocr_retry.log') with INFO level logging and a standardized timestamp format. The function is designed to provide consistent logging configuration across test modules, making it easier to debug and track test execution flow.

Source Code

def setup_test_logging():
    """Set up logging for the test."""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(sys.stdout),
            logging.FileHandler('test_ocr_retry.log')
        ]
    )
    return logging.getLogger(__name__)

Return Value

Returns a logging.Logger instance configured for the calling module (using __name__). This logger inherits the INFO level and formatting configured by basicConfig, and will output to both stdout and the 'test_ocr_retry.log' file. The logger can be used to emit log messages at various levels (debug, info, warning, error, critical).

Dependencies

  • logging
  • sys

Required Imports

import logging
import sys

Usage Example

import logging
import sys

def setup_test_logging():
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(sys.stdout),
            logging.FileHandler('test_ocr_retry.log')
        ]
    )
    return logging.getLogger(__name__)

# Usage in a test file
logger = setup_test_logging()
logger.info('Starting test execution')
logger.warning('This is a warning message')
logger.error('An error occurred during testing')

Best Practices

  • Call this function once at the beginning of your test module or test suite to avoid duplicate handler registration
  • Be aware that logging.basicConfig() only works on the first call - subsequent calls have no effect unless force=True is used (Python 3.8+)
  • Ensure the directory where the script runs has write permissions for creating 'test_ocr_retry.log'
  • Consider using a unique log filename per test run to avoid log file conflicts in parallel test execution
  • The function uses __name__ which will reflect the module where the function is defined, not where it's called from - consider passing module name as parameter for more flexibility
  • Remember to close or clean up log files after tests complete to avoid resource leaks in long-running test suites

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_test_logging 92.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_local_document.py
  • function setup_test_logging_v4 91.9% 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_logging_v4 88.8% 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_v1 88.8% 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_v2 85.3% 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