🔍 Code Extractor

function setup_test_logging_v1

Maturity: 42

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

File:
/tf/active/vicechatdev/contract_validity_analyzer/test_single_document.py
Lines:
24 - 34
Complexity:
simple

Purpose

This function sets up a standardized logging configuration specifically for testing purposes. It creates a logger that outputs to both stdout (console) and a file named 'test_single_document.log'. The logging format includes timestamp, logger name, log level, and message. This is useful for debugging tests and maintaining a persistent record of test execution.

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

Return Value

Returns a logging.Logger instance configured with the name of the calling module (__name__). This logger inherits the configuration set by basicConfig, including the INFO level and the specified format and handlers.

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

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

Best Practices

  • Call this function only once at the beginning of your test suite to avoid duplicate handlers
  • Be aware that logging.basicConfig() only works if the root logger has not been configured yet
  • The log file 'test_single_document.log' will be created in the current working directory
  • Consider using a unique log filename for parallel test execution to avoid conflicts
  • The function returns a logger with __name__ from the calling module, so the logger name will vary based on where it's called from
  • For production code, consider using a more robust logging setup utility rather than this test-specific function

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function setup_test_logging 93.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_logging_v4 91.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_v4 90.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_simulated_document.py
  • function setup_test_logging_v2 89.8% 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
  • 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
← Back to Browse