Skip to content

重构日志系统:使用 structlog 替代 loguru#1035

Merged
BBleae merged 4 commits into
devfrom
dev-refactor-logger
Jun 11, 2025
Merged

重构日志系统:使用 structlog 替代 loguru#1035
BBleae merged 4 commits into
devfrom
dev-refactor-logger

Conversation

@BBleae
Copy link
Copy Markdown
Member

@BBleae BBleae commented Jun 11, 2025

Summary

• 将日志系统从 loguru 重构为 structlog,提供更好的结构化日志支持和性能
• 统一所有文件中的日志导入,从 src.common.logger_manager 改为 src.common.logger
• 更新日志级别调用,将 logger.success 改为 logger.infologger.warning 改为 logger.warn

Test plan

  • 确认所有模块的日志功能正常工作
  • 验证日志格式和输出符合预期
  • 测试不同日志级别的输出

🤖 Generated with Claude Code

@BBleae
Copy link
Copy Markdown
Member Author

BBleae commented Jun 11, 2025

备注

  • 用 structlog 代替loguru
    • 这应该能解决目前loguru的文件句柄问题
  • 移除了上千行的样式定义
    • 控制台的日志输出仍然相对美观(?
  • (几乎)没有改变get_logger的用法
    • 其他模块可以无缝迁移到使用新的 logger
  • 修改部分日志的level

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @BBleae, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request undertakes a significant refactoring of the application's logging system, migrating from loguru to structlog. This change aims to provide more robust structured logging and potentially better performance. Alongside the library swap, the PR standardizes logger imports and adjusts log level usage across numerous files. It also updates how global singleton instances are accessed.

Highlights

  • Logging Library Migration: Replaced the loguru logging library with structlog to leverage its structured logging capabilities and improve performance.
  • Unified Logger Import: Standardized the logger import across the codebase, changing all instances from src.common.logger_manager to src.common.logger.
  • Log Level Renaming: Updated log level calls to align with standard logging practices, specifically changing logger.success to logger.info and logger.warning to logger.warn.
  • Global Instance Access: Replaced direct access to global instances like global_api and global_server with calls to their respective getter functions (get_global_api(), get_global_server()) for better encapsulation and potential lazy initialization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully refactors the logging system from loguru to structlog, unifying logger imports and updating log level calls as described. The changes are extensive and touch many files, primarily involving import path updates and method name changes for logging.

Key observations:

  • The core logging logic in src/common/logger.py has been completely rewritten using structlog. The new implementation is simpler and sets a foundation for structured logging. However, it currently lacks features like file logging and the extensive custom console styling present in the previous loguru setup. TODOs for these are noted in the new logger file.
  • A significant behavioral change was identified in bot.py concerning the handling of a missing .env file: it now logs a warning instead of raising an error. This needs clarification to ensure it aligns with application requirements.
  • The introduction of getter functions (get_global_server(), get_global_api()) for global instances is a positive change, promoting better management of these singletons.

Overall, the PR achieves its main refactoring goal. The primary points for consideration are the implications of the simplified logging configuration (loss of custom styles, pending features like file logging) and the behavioral change in .env file handling.

@BBleae BBleae requested a review from Copilot June 11, 2025 06:58
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the logging system by replacing loguru with structlog, updating logger imports and log level calls for more structured logging and improved performance.

  • Changed logger imports from src.common.logger_manager to src.common.logger
  • Replaced logger.success calls with logger.info and logger.warning calls with logger.warn
  • Updated global logger/server calls accordingly

Reviewed Changes

Copilot reviewed 132 out of 132 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/chat/focus_chat/heartFC_sender.py Updated logger import and log level call in send_message; review logger initialization.
src/chat/focus_chat/heartFC_chat.py Switched logger import to new module.
src/chat/focus_chat/heartFC_Cycleinfo.py Changed logger import and maintained logger naming.
src/chat/focus_chat/expressors/*.py Updated logger import in multiple expressor files.
src/chat/emoji_system/emoji_manager.py Modified logging level calls and import replacement.
src/chat/command/command_handler.py Updated logger import.
src/chat/actions/*.py Refactored logger import across actions modules.
src/api/*.py Replaced logger and global server imports.
scripts/*.py Updated logger import across multiple scripts.
docs/plugin_quick_start.md Changed logger import for documentation examples.
bot.py Updated logger calls (e.g., logger.success to logger.info) and removed redundant load_env call.
Comments suppressed due to low confidence (1)

src/chat/focus_chat/heartFC_sender.py:24

  • The send_message function uses 'logger.info(...)' without initializing a logger instance in this file. Consider initializing a logger (e.g., by adding 'logger = get_logger("heartFC_sender")') so that logging calls work as expected.
await get_global_api().send_message(message)

@BBleae
Copy link
Copy Markdown
Member Author

BBleae commented Jun 11, 2025

@sourcery-ai review

@BBleae
Copy link
Copy Markdown
Member Author

BBleae commented Jun 11, 2025

(generated by claude code)

Code Review - Logger Refactoring

Review Summary: APPROVED

This PR successfully refactors the logging system from loguru to structlog. The migration is well-executed and complete.

What Was Reviewed:

  1. Import Migration: ✅ All files correctly updated from src.common.logger_manager to src.common.logger
  2. Logger Method Updates: ✅ All logger.success() calls converted to logger.info()
  3. Dependencies: ✅ requirements.txt properly updated (structlog added, loguru removed)
  4. New Logger Implementation: ✅ Well-structured with appropriate processors

Key Strengths:

  • Complete migration: No remaining references to old logger system
  • Consistent method usage: All logger calls now use standard methods
  • Good structlog configuration: Includes contextual logging, timestamps, and console rendering
  • Clean code: No legacy code left behind

Technical Details:

The new src/common/logger.py provides:

  • Structured logging with contextvars support
  • Proper exception and stack info handling
  • Timestamped output with local timezone
  • Logger name binding for better traceability

Future Improvements (TODOs noted):

  • Customizable configuration
  • Log compression
  • JSON file output

Recommendation: ✅ APPROVE

This refactoring improves the logging infrastructure while maintaining full functionality. The code is ready to merge.

@BBleae BBleae merged commit 6d1f5be into dev Jun 11, 2025
3 checks passed
@BBleae BBleae deleted the dev-refactor-logger branch June 11, 2025 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants