Skip to content

Commit 9cca690

Browse files
shinonomeowclaude
andcommitted
fix: log level setting not taking effect after restart
- Import setup_logger in program.py and reapply log configuration after settings load - Modify setup_logger to properly handle reconfiguration by updating existing handlers - Ensure debug logging works correctly after restart when debug_enable is set 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9e2551c commit 9cca690

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

backend/src/module/conf/log.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ def setup_logger(level: int = logging.INFO, reset: bool = False):
1919
logging.addLevelName(logging.WARNING, "WARNING:")
2020
LOGGING_FORMAT = "[%(asctime)s] %(levelname)-8s %(message)s"
2121
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
22-
logging.basicConfig(
23-
level=level,
24-
format=LOGGING_FORMAT,
25-
datefmt=TIME_FORMAT,
26-
encoding="utf-8",
27-
handlers=[
28-
logging.FileHandler(LOG_PATH, encoding="utf-8"),
29-
logging.StreamHandler(),
30-
],
31-
)
22+
23+
# 获取根日志器
24+
root_logger = logging.getLogger()
25+
26+
# 如果已经有处理器且不是重置模式,则更新现有配置
27+
if root_logger.handlers and not reset:
28+
# 更新根日志器的等级
29+
root_logger.setLevel(level)
30+
# 更新所有处理器的等级
31+
for handler in root_logger.handlers:
32+
handler.setLevel(level)
33+
else:
34+
# 首次配置或重置模式,使用 basicConfig
35+
logging.basicConfig(
36+
level=level,
37+
format=LOGGING_FORMAT,
38+
datefmt=TIME_FORMAT,
39+
encoding="utf-8",
40+
handlers=[
41+
logging.FileHandler(LOG_PATH, encoding="utf-8"),
42+
logging.StreamHandler(),
43+
],
44+
force=reset, # 强制重新配置如果是重置模式
45+
)
3246
loggers_to_silence = [
3347
"httpx",
3448
"httpcore",

backend/src/module/core/program.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from module.conf import VERSION, settings
3+
from module.conf import VERSION, settings, setup_logger
44

55
from .aiocore import app_core
66
from .status import ProgramStatus
@@ -56,6 +56,10 @@ async def start(self):
5656
logger.debug("[Program] loading settings...")
5757
settings.load()
5858
logger.debug("[Program] settings loaded.")
59+
logger.debug("[Program] applying log configuration...")
60+
# 重新应用日志配置以反映设置更改
61+
setup_logger(level=logging.DEBUG if settings.log.debug_enable else logging.INFO)
62+
logger.debug("[Program] log configuration applied.")
5963
logger.debug("[Program] starting application core...")
6064
await self.app_core.initialize()
6165
await self.app_core.start()

0 commit comments

Comments
 (0)