完善日志系统

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