From 12af4af14cc2bada1f9f7e50ac29c019a3b7bab5 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Thu, 5 Jan 2017 22:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=B0=E5=BD=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=88=B0MongoDB=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/VT_setting.json | 1 + vn.trader/vtConstant.py | 5 ++++- vn.trader/vtEngine.py | 22 +++++++++++++++++++++- vn.trader/vtFunction.py | 4 +++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/vn.trader/VT_setting.json b/vn.trader/VT_setting.json index a6e5a1c7..0f357364 100644 --- a/vn.trader/VT_setting.json +++ b/vn.trader/VT_setting.json @@ -4,6 +4,7 @@ "mongoHost": "localhost", "mongoPort": 27017, + "mongoLogging": true, "darkStyle": true } \ No newline at end of file diff --git a/vn.trader/vtConstant.py b/vn.trader/vtConstant.py index f081379f..2fd78b62 100644 --- a/vn.trader/vtConstant.py +++ b/vn.trader/vtConstant.py @@ -80,4 +80,7 @@ CURRENCY_USD = 'USD' # 美元 CURRENCY_CNY = 'CNY' # 人民币 CURRENCY_HKD = 'HKD' # 港币 CURRENCY_UNKNOWN = 'UNKNOWN' # 未知货币 -CURRENCY_NONE = '' # 空货币 \ No newline at end of file +CURRENCY_NONE = '' # 空货币 + +# 数据库 +LOG_DB_NAME = 'VnTrader_Log_Db' \ No newline at end of file diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index 3157f5c8..eb68d627 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -2,6 +2,7 @@ import shelve from collections import OrderedDict +from datetime import datetime from pymongo import MongoClient from pymongo.errors import ConnectionFailure @@ -22,6 +23,9 @@ class MainEngine(object): #---------------------------------------------------------------------- def __init__(self): """Constructor""" + # 记录今日日期 + self.todayDate = datetime.now().strftime('%Y%m%d') + # 创建事件引擎 self.eventEngine = EventEngine() self.eventEngine.start() @@ -236,7 +240,7 @@ class MainEngine(object): """连接MongoDB数据库""" if not self.dbClient: # 读取MongoDB的设置 - host, port = loadMongoSetting() + host, port, logging = loadMongoSetting() try: # 设置MongoDB操作的超时时间为0.5秒 @@ -246,6 +250,11 @@ class MainEngine(object): self.dbClient.server_info() self.writeLog(u'MongoDB连接成功') + + # 如果启动日志记录,则注册日志事件监听函数 + if logging: + self.eventEngine.register(EVENT_LOG, self.dbLogging) + except ConnectionFailure: self.writeLog(u'MongoDB连接失败') @@ -280,6 +289,17 @@ class MainEngine(object): collection.replace_one(flt, d, upsert) else: self.writeLog(u'数据更新失败,MongoDB没有连接') + + #---------------------------------------------------------------------- + def dbLogging(self, event): + """向MongoDB中插入日志""" + log = event.dict_['data'] + d = { + 'content': log.logContent, + 'time': log.logTime, + 'gateway': log.gatewayName + } + self.dbInsert(LOG_DB_NAME, self.todayDate, d) #---------------------------------------------------------------------- def getContract(self, vtSymbol): diff --git a/vn.trader/vtFunction.py b/vn.trader/vtFunction.py index c14286e0..c299d327 100644 --- a/vn.trader/vtFunction.py +++ b/vn.trader/vtFunction.py @@ -40,11 +40,13 @@ def loadMongoSetting(): setting = json.load(f) host = setting['mongoHost'] port = setting['mongoPort'] + logging = setting['mongoLogging'] except: host = 'localhost' port = 27017 + logging = False - return host, port + return host, port, logging #---------------------------------------------------------------------- def todayDate():