Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

Complete Loguru Migration and Code Optimization - COMPLETE ✅

Logging Implementation

  • Removed LogUtils entirely: Deleted src/utils/log_utils.py and replaced all 326 LogUtils calls with direct loguru usage
  • Consistent logger pattern: All files use logger = get_logger().bind(component="ComponentName")
  • Convenient imports: Added get_logger export to src/utils/__init__.py for simplified imports: from src.utils import get_logger
  • Log location: %LOCALAPPDATA%/Programs/MinecraftServerManager/log/YYYY-MM-DD-HH-mm.log
  • Smart cleanup mechanism: When folder exceeds 10MB, deletes oldest logs until approximately 8MB is freed (size-based instead of count-based)
  • Settings-aware filtering: Console output respects debug logging settings

Code Optimizations

  • Version parsing: Replaced custom version comparison with packaging.version.Version
  • Settings management: Consolidated 3x duplicated default settings into module constants
  • Java version parsing: Unified regex patterns, extracted helper functions
  • Performance improvements: Constants for magic numbers, efficient dict operations, cached objects

Quality Assurance

  • ✅ All 35 Python files compile successfully
  • ✅ Code review completed with all issues resolved
  • ✅ Security scan (CodeQL): 0 vulnerabilities
  • ✅ Reduced code size by ~150 lines
  • ✅ All init.py files optimized for convenient imports

Import Usage Examples

# Convenient import from src.utils
from src.utils import get_logger
logger = get_logger().bind(component="ComponentName")

# Alternative direct import
from src.utils.logger import get_logger
logger = get_logger().bind(component="ComponentName")

Recent Updates

  • Modified log cleanup mechanism to delete logs based on size (8MB target) instead of fixed count
  • Added get_logger export to src/utils/__init__.py for convenient imports
  • Updated documentation with usage examples
Original prompt

1.掃描所有src目錄中檔案,優化程式碼內容並精簡不必要的程式碼以及提高程式的效能
2.將src目錄中用來記錄log的檔案以及print出所有有關於debug/warning/error/info的部分統一改成用loguru取代,並且添加功能,會在%LOCALAPPDATA%/Programs/MinecraftServerManager/log中記錄每次開起的log,log名稱「年-月-日-時-分.log」每次執行程式時先偵測log資料夾大小,當超過10MB實則刪除最舊的10筆log紀錄,然後將原本紀錄log的檔案移除,並修改README.md以及技術手冊/使用手冊等文檔


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Colin955023 Colin955023 marked this pull request as ready for review January 24, 2026 07:13
Copilot AI review requested due to automatic review settings January 24, 2026 07:13
Copy link

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.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI and others added 3 commits January 24, 2026 07:17
…ize dict merge and path handling, update documentation

Co-authored-by: Colin955023 <[email protected]>
…, improve time constants calculation

Co-authored-by: Colin955023 <[email protected]>
Copilot AI changed the title [WIP] Optimize code and unify logging using loguru Migrate logging to loguru with automatic log rotation and optimize codebase Jan 24, 2026
Copilot AI requested a review from Colin955023 January 24, 2026 07:27
@Colin955023
Copy link
Owner

@copilot
1.全面移除logutils,全部都用loguru取代日誌紀錄包含原本logutils中用來print出訊息的部分也都用loguru取代並完整移除logutils這個檔案,其他src目錄中所有檔案也幫我尋找看看有沒有功能是已經有相關函式庫可以取代並節省檔案大小
2.優化所有程式碼並最大化提高效能
3.全檔案進行編譯檢查避免錯誤

@Colin955023
Copy link
Owner

@copilot
修改一下刪除log的機制,當偵測到log資料夾超過10MB,則刪除「數個日誌加起來相當於8MB」的舊日誌

Copilot AI and others added 3 commits January 24, 2026 07:46
Remove component parameter from logger calls since logger is already bound at module level:
- src/utils/ui_utils.py: Remove component parameter from lines 357, 364, 369, 504
- src/ui/mod_management.py: Fix logger.bind().error() pattern on lines 348-350
- src/core/server_manager.py: Remove component parameter from line 435-436
- src/ui/main_window.py: Remove component parameter from line 321
- src/ui/server_monitor_window.py: Remove component and exception parameters from lines 61-64, 118-120

Co-authored-by: Colin955023 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 24, 2026

@copilot
1.全面移除logutils,全部都用loguru取代日誌紀錄包含原本logutils中用來print出訊息的部分也都用loguru取代並完整移除logutils這個檔案,其他src目錄��...

已完成所有要求的修改:

  1. 完全移除 LogUtils:已刪除 log_utils.py 文件,所有 326 個 LogUtils 調用已替換為直接使用 loguru,每個文件使用 logger = get_logger().bind(component="...") 模式

  2. 使用函式庫取代自定義程式碼:使用 packaging.version 取代自定義版本解析,統一合併重複的設定常數

  3. 優化程式碼:合併重複程式碼(3x 預設設定 → 1x 模組常數),簡化 Java 版本解析,提取輔助函數減少重複

  4. 全檔案編譯檢查:所有 35 個 Python 文件編譯成功,CodeQL 安全掃描 0 個漏洞,程式碼審查問題已全部修復

主要改進 commit: 4acee0f, a4a799c, fa082d3

Copilot AI changed the title Migrate logging to loguru with automatic log rotation and optimize codebase Complete migration to loguru, remove LogUtils entirely, and optimize codebase Jan 24, 2026
Copy link
Contributor Author

Copilot AI commented Jan 24, 2026

@copilot
修改一下刪除log的機制,當偵測到log資料夾超過10MB,則刪除「數個日誌加起來相當於8MB」的舊日誌

已修改日誌刪除機制:當日誌資料夾超過 10MB 時,會刪除最舊的日誌直到釋放約 8MB 空間(而非固定刪除 10 筆)。

新機制會根據實際檔案大小累計刪除,更精確地控制日誌空間使用。

相關修改:

  • 更新 logger.py 刪除邏輯
  • 更新 README.md、TECHNICAL_OVERVIEW.md、USER_GUIDE.md 文檔

Commit: c8990c3

@Colin955023
Copy link
Owner

@copilot
針對src目錄下個資料夾中的__init__.py進行修改,並修改引用的內容

Copy link
Contributor Author

Copilot AI commented Jan 24, 2026

@copilot
針對src目錄下個資料夾中的__init__.py進行修改,並修改引用的內容

已修改 src/utils/init.py,移除了已過時的 LogUtils 相關匯出:

  • 移除 get_logger, log_info, log_warning, log_error, log_debug, log_error_with_exception 匯出
  • 添加說明註解,指出 logger 功能現在直接從 .logger 模組導入
  • 所有文件現在使用 from src.utils.logger import get_logger 直接導入

其他 init.py 文件(src/core、src/ui、src/models、src/version_info)已檢查,結構良好無需修改。

Commit: dcca20d

@Colin955023
Copy link
Owner

@copilot
在src/utils/init.py中添加logger的呼叫,讓其他檔案需要呼叫是可以更簡便

Copy link
Contributor Author

Copilot AI commented Jan 24, 2026

@copilot
在src/utils/init.py中添加logger的呼叫,讓其他檔案需要呼叫是可以更簡便

已在 src/utils/init.py 中添加 get_logger 匯出,現在可以更簡便地導入:

# 簡便方式
from src.utils import get_logger
logger = get_logger().bind(component="ComponentName")

# 或者原本的方式
from src.utils.logger import get_logger

兩種方式都可以使用,第一種更簡潔。已更新文檔說明。

Commit: e99b8b5

@Colin955023
Copy link
Owner

@copilot
我要的效果是可以像其他檔案一樣直接指定資料夾呼叫function
例如這樣
from src.utils import HTTPUtils, UIUtils, ensure_dir, get_cache_dir

Copy link
Contributor Author

Copilot AI commented Jan 24, 2026

@copilot
我要的效果是可以像其他檔案一樣直接指定資料夾呼叫function
例如這樣
...

目前的實作已經支援您要的效果!get_logger 已經加入 _EXPORTS 字典中,可以像其他工具函數一樣導入:

# 完全支援這種方式
from src.utils import get_logger, HTTPUtils, UIUtils, ensure_dir, get_cache_dir

# 使用範例
logger = get_logger().bind(component="MyComponent")
http_utils = HTTPUtils()

這與 HTTPUtils、UIUtils 等的導入方式完全一致,使用相同的 lazy import 機制。

Commit: e99b8b5 已實現此功能。

@Colin955023 Colin955023 merged commit fe1ab52 into master Jan 24, 2026
1 check passed
@Colin955023 Colin955023 deleted the copilot/optimize-src-code-and-logging branch January 24, 2026 09:29
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