完善日志系统

This commit is contained in:
2025-11-25 17:13:40 +08:00
parent 81da3fe013
commit 4787011177
5 changed files with 36 additions and 17 deletions
+2 -1
View File
@@ -40,7 +40,7 @@ def exist_config() -> bool:
return config_path.exists()
def initConfig() -> bool:
global miniQMTPath, account_no
global miniQMTPath, account_no, log_level
# 获取配置文件路径
config_path = get_config_path()
@@ -49,6 +49,7 @@ def initConfig() -> bool:
config.read(config_path, encoding='utf-8')
miniQMTPath = config.get('config','miniQMTPath')
account_no = config.get('config','account_no')
log_level = config.get('config','log_level')
# 判断miniQMTPath是否为空,并且目录是否存在
if not miniQMTPath or not Path(miniQMTPath).exists():
+10 -6
View File
@@ -3,12 +3,16 @@ from enum import Enum
from core.eventbus import EventPrintLog, event_bus
import config
class LogLevel(Enum):
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
DEBUG = 0
INFO = 1
WARNING = 2
ERROR = 3
CRITICAL = 4
def __le__(self, other):
return self.value <= other.value
class LogData:
def __init__(self, level:LogLevel, message:str):
@@ -19,4 +23,4 @@ def PrintLog(level:LogLevel, message:str):
data = LogData(level, message)
event_bus.publish(EventPrintLog, data)
if config.console_log:
print(f'{level.value} {message}')
print(f'{level.name} {message}')
+17 -4
View File
@@ -1,13 +1,16 @@
import tkinter as tk
from tkinter import ttk
from core.logger import LogLevel
from core.logger import LogLevel, LogData, PrintLog
from core.qmt import qmtv
from core.sfgrid.sfgrid_ui import TradeTargetUI
from tkinter import ttk, messagebox
from core.eventbus import EventPrintLog
from core.eventbus import event_bus as eBus
class MainWindow:
def __init__(self):
def __init__(self, configLogLevel:str):
self.root = tk.Tk()
self.root.title("神之一手 - 交易系统")
self.root.geometry("1400x700")
@@ -20,6 +23,11 @@ class MainWindow:
self.log_visible = False
self.create_ui()
self.logLevel = LogLevel[configLogLevel]
PrintLog(LogLevel.DEBUG, f"系统启动成功 {self.logLevel.name}")
eBus.subscribe(EventPrintLog, self.on_log_event)
def create_ui(self):
"""创建UI界面"""
@@ -40,7 +48,7 @@ class MainWindow:
# 创建Tab按钮(垂直排列,文字垂直显示)
self.tab_buttons = []
strategy_names = ["蒙派", "复盘"]
strategy_names = ["网格", "复盘"]
for idx, name in enumerate(strategy_names):
btn = ttk.Button(
@@ -168,11 +176,16 @@ class MainWindow:
self.log_table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
def on_log_event(self, event:LogData):
if self.logLevel.value <= event.level.value:
self.add_log(event.level, event.message)
def add_log(self, level:LogLevel, message):
"""添加日志记录 - 全局方法"""
from datetime import datetime
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.log_table.insert('', 0, values=(timestamp, level.value, message))
self.log_table.insert('', 0, values=(timestamp, level.name, message))
def clear_logs(self):
"""清空日志记录"""
+5 -4
View File
@@ -94,7 +94,7 @@ class QmtV(XtQuantTraderCallback):
def dailyUpStop(self, stock_code:str):
cacheStock = self.cacheStockDetail(stock_code)
PrintLog(LogLevel.INFO, f'- [成功]获取股票详情: {stock_code} {cacheStock["InstrumentName"]} {cacheStock['UpStopPrice']}')
PrintLog(LogLevel.INFO, f'- [成功]获取股票详情: {stock_code} {cacheStock["InstrumentName"]} {cacheStock["UpStopPrice"]}')
return cacheStock['UpStopPrice']
def dailyDownStop(self, stock_code:str):
@@ -134,10 +134,11 @@ class QmtV(XtQuantTraderCallback):
tmpMarketStatus = self.isMarketActive
PrintLog(LogLevel.INFO, f'- [市场状态变更] {self.isMarketActive}')
eBus.event_bus.publish(eBus.EventMarketActiveSwitch, self.isMarketActive)
if tmpMarketStatus and self.isMarketActive:
if tmpTime - self.lastMarketDataUpdateTimestamp > 10: # 上次更新市场状态已经超过10秒
if tmpMarketStatus and self.isMarketActive and tmpTime - self.lastMarketDataUpdateTimestamp > 10: # 上次更新市场状态已经超过10秒
self.isMarketActive = False
PrintLog(LogLevel.INFO, f'- [市场状态] {self.isMarketActive}') # 市场已 inactive
PrintLog(LogLevel.INFO, f'- [市场状态变更] {self.isMarketActive}')
PrintLog(LogLevel.DEBUG, f'- [市场状态] {self.isMarketActive}') # 市场已 inactive
# ====== 市场回调方法 -- 以下方法由XtQuantTrader调用 ======
+1 -1
View File
@@ -131,7 +131,7 @@ def initialize_system():
connected = qmtv.connect()
if connected:
# 连接成功,启动主窗口
window = MainWindow()
window = MainWindow(sdConstants.log_level)
window.run()
break
else: