Files
TestGit/1.py
2025-11-07 00:23:48 +13:00

38 lines
1.0 KiB
Python
Raw Permalink 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
from datetime import datetime
from pathlib import Path
import cv22
# pip freeze > requirements.txt
# pip install -r requirements.txt
# 创建日志目录(如果不存在)
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
# 生成带时间戳的日志文件名
log_filename = log_dir / f"app_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
# 配置日志系统
logging.basicConfig(
level=logging.INFO, # 设置日志级别可选DEBUG, INFO, WARNING, ERROR, CRITICAL
format="%(asctime)s [%(levelname)s] %(message)s", # 日志格式
handlers=[
logging.FileHandler(log_filename, encoding='utf-8'), # 输出到文件
logging.StreamHandler() # 同时输出到控制台
]
)
# 示例日志
logging.debug("这是调试信息(不会显示,因为默认级别是 INFO")
logging.info("程序启动")
logging.warning("警告:某个操作可能有风险")
logging.error("错误:出现异常")
logging.critical("严重错误:程序即将终止")
print(f"日志文件已创建:{log_filename}")