Files
ai-titration/utils.py
flt6 be0ba3a3d6 use logging instead print
Former-commit-id: bff176e0e99d1c50f48a63a3fd30adcee67777a8
2025-05-26 23:26:31 +08:00

58 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import os
from datetime import datetime
def setup_logging(log_level=logging.INFO, log_dir="logs"):
"""
设置logging配置创建不同模块的logger
Args:
log_level: 日志级别默认INFO
log_dir: 日志文件存储目录,默认"logs"
"""
# 创建日志目录
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# 获取当前时间作为日志文件名
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
log_file = os.path.join(log_dir, f"titration_{timestamp}.log")
# 配置根logger
logging.basicConfig(
level=log_level,
format='%(asctime)s - %(name)8s - %(levelname)7s - %(message)s',
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler() # 同时输出到控制台
]
)
return log_file
def get_system_logger():
"""系统初始化和控制相关的logger"""
return logging.getLogger("System")
def get_hardware_logger():
"""硬件控制相关的loggerCH340等"""
return logging.getLogger("Hardware")
def get_vision_logger():
"""图像处理和预测相关的logger"""
return logging.getLogger("Vision")
def get_control_logger():
"""控制逻辑相关的logger"""
return logging.getLogger("Control")
def get_endpoint_logger():
"""终点检测相关的logger"""
return logging.getLogger("Endpoint")
def get_volume_logger():
"""体积计算相关的logger"""
return logging.getLogger("Volume")