chore: keep traceback out of debug logs

This commit is contained in:
liuwei
2026-04-13 15:05:13 +08:00
parent ce9726d37d
commit fc43321f94

28
main.py
View File

@@ -13,6 +13,19 @@ from loguru import logger
from utils.sehuatang.sehuatang_bot import SehuatangCrawler
# 普通日志不附带 traceback避免 debug/info 文件被异常堆栈刷屏。
def _plain_log_format(record):
record["exception"] = None
return "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level:<8} | {name}:{function}:{line} - {message}\n"
def _error_log_format(record):
return (
"{time:YYYY-MM-DD HH:mm:ss.SSS} | {level:<8} | {name}:{function}:{line} - {message}\n"
"{exception}"
)
# INFO 日志(包含 INFO、DEBUG但不包含 WARNING、ERROR
logger.add(
f"logs/wx_info.log",
@@ -20,7 +33,10 @@ logger.add(
filter=lambda record: record["level"].name in ["INFO", "DEBUG"],
rotation="10 MB",
retention="7 days",
encoding="utf-8"
encoding="utf-8",
format=_plain_log_format,
backtrace=False,
diagnose=False,
)
# ERROR 日志(仅 ERROR 及以上)
@@ -29,7 +45,10 @@ logger.add(
level="ERROR",
rotation="10 MB",
retention="7 days",
encoding="utf-8"
encoding="utf-8",
format=_error_log_format,
backtrace=True,
diagnose=True,
)
# ERROR 日志(仅 ERROR 及以上)
logger.add(
@@ -37,7 +56,10 @@ logger.add(
level="DEBUG",
rotation="10 MB",
retention="7 days",
encoding="utf-8"
encoding="utf-8",
format=_plain_log_format,
backtrace=False,
diagnose=False,
)