36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import logging
|
|
import os
|
|
from datetime import datetime
|
|
|
|
def setup_logger(output_dir):
|
|
# 创建logs目录
|
|
log_dir = os.path.join(output_dir, 'logs')
|
|
os.makedirs(log_dir, exist_ok=True)
|
|
|
|
# 创建日志文件名(包含时间戳)
|
|
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
log_file = os.path.join(log_dir, f'preprocess_{timestamp}.log')
|
|
|
|
# 配置日志格式
|
|
formatter = logging.Formatter(
|
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
)
|
|
|
|
# 配置文件处理器
|
|
file_handler = logging.FileHandler(log_file, encoding='utf-8')
|
|
file_handler.setFormatter(formatter)
|
|
|
|
# 配置控制台处理器
|
|
console_handler = logging.StreamHandler()
|
|
console_handler.setFormatter(formatter)
|
|
|
|
# 获取根日志记录器
|
|
logger = logging.getLogger('UAV_Preprocess')
|
|
logger.setLevel(logging.INFO)
|
|
|
|
# 添加处理器
|
|
logger.addHandler(file_handler)
|
|
logger.addHandler(console_handler)
|
|
|
|
return logger |