2.1
This commit is contained in:
39
1.py
39
1.py
@@ -1,2 +1,37 @@
|
||||
print("hello")
|
||||
print("leo")
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
import cv2
|
||||
|
||||
# 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}")
|
||||
|
||||
Reference in New Issue
Block a user