function setup_test_logging_v2
Configures Python logging for test execution with both console and file output handlers.
/tf/active/vicechatdev/contract_validity_analyzer/test_missing_end_dates.py
35 - 45
simple
Purpose
This function sets up a standardized logging configuration specifically for test environments. It creates a logger that outputs to both stdout (console) and a file named 'test_missing_end_dates.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_missing_end_dates.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 basicConfig settings (INFO level, formatted output, dual handlers) and can be used throughout the test to log messages at various levels (info, warning, error, etc.).
Dependencies
loggingsys
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_missing_end_dates.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 once at the beginning of your test suite or test module to avoid duplicate handlers
- 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 log file will be created has write permissions
- Consider using a unique log filename for different test modules to avoid log file conflicts
- The log file will be created in the current working directory; consider using an absolute path or a dedicated logs directory for better organization
- Remember to close or clean up log files after tests complete if running in CI/CD environments
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function setup_test_logging_v1 89.8% similar
-
function setup_test_logging 88.2% similar
-
function setup_logging_v4 87.1% similar
-
function setup_test_logging_v4 86.6% similar
-
function setup_test_logging_v3 85.3% similar