🔍 Code Extractor

function setup_test_logging_v4

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_simulated_document.py
Lines:
37 - 47
Complexity:
simple

Purpose

This function initializes a standardized logging configuration for test scenarios. It sets up dual output streams (stdout and file) with INFO level logging and a consistent timestamp format. The function is designed to be called at the beginning of test execution to ensure all test activities are properly logged for debugging and monitoring purposes.

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_simulated_document.log')
        ]
    )
    return logging.getLogger(__name__)

Return Value

Returns a logging.Logger instance configured for the calling module (using __name__). This logger inherits the basicConfig settings (INFO level, formatted output to both console and 'test_simulated_document.log' file) and can be used throughout the test to log messages at various severity levels.

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_simulated_document.log')
        ]
    )
    return logging.getLogger(__name__)

# Usage in test file
logger = setup_test_logging()
logger.info('Test started')
logger.warning('This is a warning')
logger.error('An error occurred')

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 takes effect on the first call; subsequent calls have no effect unless force=True is used (Python 3.8+)
  • Ensure the directory where the log file will be created has write permissions
  • Consider using a unique log filename for different test modules to avoid log file conflicts when running tests in parallel
  • The function uses __name__ which will be the module name where the function is defined, not where it's called from; consider passing module name as parameter if needed
  • Remember to close or clean up the log file after tests complete if disk space is a concern

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_test_logging 93.7% 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 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_ocr_retry.py
  • function setup_test_logging_v1 90.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_logging_v4 90.4% 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_v2 86.6% 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