refactor(logging): 新增项目运行态日志分流能力#2199
Conversation
- 在框架层新增项目运行态日志分流能力 - 保持默认 OneDragon 日志行为不变,由项目显式启用分流 - 使用 delay=True 减少日志文件提前占用
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 Walkthrough漫游引入按运行时划分的日志配置:新增配置驱动的日志工具和文档,提供 变更
序列图sequenceDiagram
participant App as 应用程序
participant Config as configure_project_runtime_logging()
participant ProjLogger as 项目日志记录器
participant FwLogger as 框架日志记录器
participant FileHandler as TimedRotatingFileHandler
participant Console as 控制台处理程序
participant Context as ProjectRuntimeLoggingContext
App->>Config: 调用 configure_project_runtime_logging(project_logger_name, project_log_file_path, framework_log_file_path, ...)
Config->>ProjLogger: get_or_create_logger(project_name) / configure_logger(...)
Config->>FwLogger: get_or_create_logger(framework_name) / configure_logger(...)
Config->>FileHandler: 为项目/框架创建带所有权标记的文件处理程序
FileHandler-->>ProjLogger: 附加至项目日志器(内部所有)
FileHandler-->>FwLogger: 附加至框架日志器(内部所有)
alt 控制台启用
Config->>Console: 创建控制台处理程序
Console-->>ProjLogger: 可选附加
Console-->>FwLogger: 可选附加
end
Config->>Context: 返回包含 logger 实例与文件路径的 ProjectRuntimeLoggingContext
Context-->>App: 应用开始后使用返回的项目/框架日志记录器写日志
预估代码审查工作量🎯 4 (Complex) | ⏱️ ~45 minutes 诗
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/one_dragon/utils/log_utils.py (2)
132-144: 建议改用pathlib处理路径。仓库规范要求使用
pathlib而非os.path。当前get_log_file_path通过os.path.isabs和os.path.join进行拼接,可以改成pathlib.Path,对外仍返回str,行为保持一致。♻️ 参考改写
-def get_log_file_path(log_file_path: str | None = None, default_name: str = 'log.txt') -> str: +def get_log_file_path(log_file_path: str | None = None, default_name: str = 'log.txt') -> str: """获取日志文件路径。 - 未传 `log_file_path` 时,使用工作目录 `.log/` 下的默认文件名 - 传相对路径/文件名时,仍然放在工作目录 `.log/` 下 - 传绝对路径时,直接使用 """ configured = (log_file_path or '').strip() if not configured: configured = default_name - if os.path.isabs(configured): - return configured - return os.path.join(os_utils.get_path_under_work_dir('.log'), configured) + path = Path(configured) + if path.is_absolute(): + return str(path) + return str(Path(os_utils.get_path_under_work_dir('.log')) / path)并在文件顶部增加:
import logging -import os +from pathlib import Path from contextlib import suppress(如其它处仍依赖
os,可保留该导入。)As per coding guidelines: “Use
pathliblibrary for path handling instead ofos.path”.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/one_dragon/utils/log_utils.py` around lines 132 - 144, Replace os.path usage in get_log_file_path with pathlib: create a Path from the configured string (after applying (log_file_path or '').strip() and default_name fallback), check absoluteness with Path.is_absolute(), and when not absolute join it with the Path returned by os_utils.get_path_under_work_dir('.log') (wrap that in Path(...)) using / or joinpath; finally return str(...) to preserve the existing string return type. Ensure the function name get_log_file_path, the default_name behavior, and os_utils.get_path_under_work_dir call are preserved and that other imports remain if still needed.
147-152:get_logger缺少返回值类型注解。-def get_logger(): +def get_logger() -> logging.Logger: """获取框架默认 logger。As per coding guidelines: “All functions and methods must include type hints”.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/one_dragon/utils/log_utils.py` around lines 147 - 152, The function get_logger currently lacks a return type annotation; update its signature to include the proper return type hint (the logger type returned by get_or_create_logger) so it matches the project's typing rules — locate get_logger and add a return annotation referencing the logger class/type used by get_or_create_logger (ensure compatibility with LOGGER_NAME and LoggerConfig usage), and adjust any imports if needed to provide the logger type for the annotation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/develop/one_dragon/modules/logger.md`:
- Around line 91-122: The documentation currently references two different
import paths for the same runner logging adapter; unify them to the actual
module path used in the project (the module that defines RUNNER_LOGGER_NAME,
RUNNER_LOG_FILE_NAME, RUNNER_FRAMEWORK_LOG_FILE_NAME, the log logger, and
configure_runner_runtime_logging). Update the occurrences of
`script_chainer.win_exe.runner_logging` and
`script_chainer.utils.runner_logging` so they both use the real import path that
contains configure_runner_runtime_logging and log, and ensure the code example
and the explanatory text consistently show that same module path.
---
Nitpick comments:
In `@src/one_dragon/utils/log_utils.py`:
- Around line 132-144: Replace os.path usage in get_log_file_path with pathlib:
create a Path from the configured string (after applying (log_file_path or
'').strip() and default_name fallback), check absoluteness with
Path.is_absolute(), and when not absolute join it with the Path returned by
os_utils.get_path_under_work_dir('.log') (wrap that in Path(...)) using / or
joinpath; finally return str(...) to preserve the existing string return type.
Ensure the function name get_log_file_path, the default_name behavior, and
os_utils.get_path_under_work_dir call are preserved and that other imports
remain if still needed.
- Around line 147-152: The function get_logger currently lacks a return type
annotation; update its signature to include the proper return type hint (the
logger type returned by get_or_create_logger) so it matches the project's typing
rules — locate get_logger and add a return annotation referencing the logger
class/type used by get_or_create_logger (ensure compatibility with LOGGER_NAME
and LoggerConfig usage), and adjust any imports if needed to provide the logger
type for the annotation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 81ce3003-5d7a-45c1-8b45-2f17df9f4e51
📒 Files selected for processing (2)
docs/develop/one_dragon/modules/logger.mdsrc/one_dragon/utils/log_utils.py
Summary by CodeRabbit
发布说明
新功能
文档