function setup_logging_v2
Configures Python's logging system with both console and file output, creating a timestamped log file for real document testing sessions.
/tf/active/vicechatdev/contract_validity_analyzer/test_real_documents.py
23 - 35
simple
Purpose
This function initializes a comprehensive logging setup for document testing workflows. It creates a dual-output logging configuration that writes DEBUG-level messages to both the console (stdout) and a timestamped log file. The log file naming convention includes 'real_doc_test_' prefix followed by a timestamp, making it easy to track and organize logs from different test runs. This is particularly useful for debugging document analysis processes and maintaining audit trails of test executions.
Source Code
def setup_logging():
"""Setup detailed logging for real document testing."""
log_filename = f"real_doc_test_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler(log_filename)
]
)
return log_filename
Return Value
Returns a string containing the generated log filename in the format 'real_doc_test_YYYYMMDD_HHMMSS.log', where the timestamp represents when the function was called. This filename can be used for reference, cleanup, or further file operations.
Dependencies
loggingdatetime
Required Imports
import logging
from datetime import datetime
Usage Example
import logging
from datetime import datetime
def setup_logging():
log_filename = f"real_doc_test_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler(log_filename)
]
)
return log_filename
# Usage
log_file = setup_logging()
print(f"Logging to: {log_file}")
logging.debug("This is a debug message")
logging.info("Processing document...")
logging.error("An error occurred")
Best Practices
- Call this function once at the start of your application or test suite, before any logging operations
- Be aware that calling logging.basicConfig() multiple times has no effect after the first call unless you reset the logging configuration
- Store the returned log_filename if you need to reference, move, or delete the log file later
- Consider adding error handling around file creation in production environments to handle permission issues
- The function creates log files in the current working directory; consider specifying an absolute path or logs directory for production use
- Log files accumulate over time; implement a cleanup strategy to manage disk space
- The DEBUG level logs all messages; consider making the log level configurable for production environments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function setup_test_logging 85.9% similar
-
function setup_logging_v3 84.9% similar
-
function setup_test_logging_v1 84.0% similar
-
function setup_test_logging_v4 81.3% similar
-
function setup_test_logging_v3 80.5% similar