首次提交完整的python工程代码
This commit is contained in:
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
BIN
utils/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
utils/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/checksum.cpython-311.pyc
Normal file
BIN
utils/__pycache__/checksum.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/hex_helper.cpython-311.pyc
Normal file
BIN
utils/__pycache__/hex_helper.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/logger.cpython-311.pyc
Normal file
BIN
utils/__pycache__/logger.cpython-311.pyc
Normal file
Binary file not shown.
2
utils/checksum.py
Normal file
2
utils/checksum.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def calc_sum(data: bytes) -> int:
|
||||
return sum(data) & 0xFFFF
|
||||
5
utils/hex_helper.py
Normal file
5
utils/hex_helper.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from utils.logger import logger
|
||||
|
||||
def hex_dump(data: bytes, title: str):
|
||||
s = data.hex(" ").upper()
|
||||
logger.info(f"[{title}] len={len(data)} | {s}")
|
||||
73
utils/logger.py
Normal file
73
utils/logger.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
|
||||
if not os.path.exists("log"):
|
||||
os.mkdir("log")
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||
handlers=[
|
||||
logging.FileHandler("log/forward.log", encoding="utf-8"),
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
|
||||
logger = logging.getLogger("Forward")
|
||||
|
||||
"""
|
||||
"""
|
||||
2026/4/18
|
||||
Tan Mingyan
|
||||
原生 logging 模块、按天切割、保留最近 7 天、自动删除旧日志 的方案,零第三方库、直接替换即用,
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from logging.handlers import TimedRotatingFileHandler
|
||||
|
||||
def get_exe_dir():
|
||||
"""获取程序根目录(打包+开发都通用)"""
|
||||
if getattr(sys, 'frozen', False):
|
||||
return os.path.dirname(sys.executable)
|
||||
else:
|
||||
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# 日志目录(和exe同一级的logs文件夹)
|
||||
log_dir = os.path.join(get_exe_dir(), "logs")
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
# 日志文件路径
|
||||
log_file = os.path.join(log_dir, "forward.log")
|
||||
|
||||
# 创建 logger
|
||||
logger = logging.getLogger("ProtocolForward")
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.handlers.clear() # 避免重复打印
|
||||
|
||||
# 格式
|
||||
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# ====================== 核心:按天分割日志 ======================
|
||||
# when="midnight" 每天0点切割
|
||||
# backupCount=7 保留最近7天日志,自动删除旧的
|
||||
file_handler = TimedRotatingFileHandler(
|
||||
log_file,
|
||||
when="midnight",
|
||||
interval=1,
|
||||
backupCount=7,
|
||||
encoding="utf-8",
|
||||
delay=True
|
||||
)
|
||||
file_handler.setFormatter(formatter)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
# 控制台打印(可选)
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setFormatter(formatter)
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
# 禁止日志传递
|
||||
logger.propagate = False
|
||||
Reference in New Issue
Block a user