Formatting
This commit is contained in:
44
utils/log_utils.py
Normal file
44
utils/log_utils.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def setup_logging(log_dir, config_name):
|
||||
"""Configures logging to output to both file and console.
|
||||
|
||||
Args:
|
||||
log_dir (str): The directory where the log file should be saved.
|
||||
config_name (str): The name of the configuration run, used for the log filename.
|
||||
"""
|
||||
# Ensure log directory exists
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
log_filename = f"{config_name}_train.log"
|
||||
log_filepath = os.path.join(log_dir, log_filename)
|
||||
|
||||
# Configure the root logger
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, # Log INFO level and above (INFO, WARNING, ERROR, CRITICAL)
|
||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
handlers=[
|
||||
logging.FileHandler(log_filepath), # Log to a file
|
||||
logging.StreamHandler(sys.stdout), # Log to the console (stdout)
|
||||
],
|
||||
# Force=True ensures that if basicConfig was called before (e.g., by a library),
|
||||
# this configuration will overwrite it. Use with caution if libraries might
|
||||
# configure logging themselves in complex ways.
|
||||
force=True,
|
||||
)
|
||||
|
||||
logging.info(f"Logging configured. Log file: {log_filepath}")
|
||||
|
||||
|
||||
# Example usage (can be removed or kept for testing):
|
||||
if __name__ == "__main__":
|
||||
print("Testing logging setup...")
|
||||
setup_logging("temp_logs", "test_config")
|
||||
logging.info("This is an info message.")
|
||||
logging.warning("This is a warning message.")
|
||||
logging.error("This is an error message.")
|
||||
print("Check 'temp_logs/test_config_train.log' and console output.")
|
||||
Reference in New Issue
Block a user