输出改用logging
This commit is contained in:
parent
6547b5cca4
commit
45a904d8c7
|
@ -1,6 +1,8 @@
|
|||
# -*- coding:utf-8 -*-
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import fileDataCreating
|
||||
import global_var
|
||||
|
||||
|
@ -19,7 +21,7 @@ ui_file_patch = path.parent.parent / "ui" / "ui_main.ui"
|
|||
# 从文件中加载UI定义
|
||||
formType, baseType = loadUiType(str(ui_file_patch))
|
||||
|
||||
# 重定向输出
|
||||
# 控制台重定向到GUI
|
||||
class EmittingStr(QObject):
|
||||
# 定义信号
|
||||
textWritten = Signal(str)
|
||||
|
@ -32,6 +34,11 @@ class EmittingStr(QObject):
|
|||
loop.exec_()
|
||||
QApplication.processEvents()
|
||||
|
||||
# logging重定向到print
|
||||
class PrintHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
# 处理文件的线程
|
||||
class FileThread(QThread):
|
||||
|
@ -58,6 +65,15 @@ class Window(formType, baseType):
|
|||
sys.stdout = EmittingStr()
|
||||
sys.stdout.textWritten.connect(self.outputWritten)
|
||||
|
||||
# 创建并配置logger
|
||||
self.logger = logging.getLogger()
|
||||
self.logger.setLevel(logging.INFO)
|
||||
# 创建并配置处理程序
|
||||
handler = PrintHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
||||
self.logger.addHandler(handler)
|
||||
|
||||
|
||||
# 输出到GUI,信号处理函数
|
||||
def outputWritten(self, text):
|
||||
cursor = self.Log_Output.textCursor()
|
||||
|
@ -69,7 +85,7 @@ class Window(formType, baseType):
|
|||
|
||||
@Slot() #声明槽函数
|
||||
def start(self):
|
||||
print('点击了开始按钮')
|
||||
self.logger.info("点击了开始按钮")
|
||||
# 输入框参数校验
|
||||
num = self.tabWidget.currentIndex()
|
||||
if num == 0:
|
||||
|
@ -87,7 +103,7 @@ class Window(formType, baseType):
|
|||
|
||||
@Slot()
|
||||
def stop(self):
|
||||
print('点击了停止按钮')
|
||||
self.logger.info("点击了停止按钮")
|
||||
num = self.tabWidget.currentIndex()
|
||||
if num == 0:
|
||||
self.stop_run_file()
|
||||
|
@ -99,10 +115,10 @@ class Window(formType, baseType):
|
|||
|
||||
@Slot()
|
||||
def conntest(self):
|
||||
print('点击了测试连接按钮')
|
||||
self.logger.info("点击了测试连接按钮")
|
||||
self.check_db_input()
|
||||
# TODO(MH):校验连接信息
|
||||
print('数据库连接成功')
|
||||
self.logger.info("数据库连接成功")
|
||||
|
||||
|
||||
def setup_thread(self):
|
||||
|
@ -120,17 +136,17 @@ class Window(formType, baseType):
|
|||
|
||||
|
||||
def check_file_input(self):
|
||||
print('文件参数校验通过')
|
||||
self.logger.info("文件参数校验通过")
|
||||
|
||||
|
||||
def check_db_input(self):
|
||||
print('数据库参数校验通过,开始连接数据库测试。。。')
|
||||
self.logger.info("数据库参数校验通过,开始连接数据库测试。。。")
|
||||
|
||||
|
||||
def start_run_file(self):
|
||||
|
||||
# 调用文件处理脚本
|
||||
print('开始生成文件。。。')
|
||||
self.logger.info("开始生成文件。。。")
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ import csv
|
|||
import yaml
|
||||
import time
|
||||
import global_var
|
||||
import logging
|
||||
|
||||
|
||||
from schedule import every, repeat, run_pending
|
||||
from pathlib import Path
|
||||
|
@ -24,6 +26,19 @@ from random import choice
|
|||
"""
|
||||
|
||||
|
||||
class PrintHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
msg = self.format(record)
|
||||
print(msg)
|
||||
|
||||
# 创建并配置logger
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.DEBUG)
|
||||
# 创建并配置处理程序
|
||||
handler = PrintHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
|
||||
# 定义公共的部分
|
||||
path = Path(__file__)
|
||||
etc_dir = path.parent.parent / "etc"
|
||||
|
@ -64,7 +79,7 @@ def file_info():
|
|||
# 每小时生成一个csv文件
|
||||
# @repeat(every(1).hours)--MH:这个是基于运行的时间,不是基于系统时间
|
||||
def new():
|
||||
print('new...')
|
||||
logger.info('new...')
|
||||
# 创建文件夹
|
||||
finfo = file_info()
|
||||
if not finfo[2].exists():
|
||||
|
@ -73,19 +88,19 @@ def new():
|
|||
faker_data.save_data_csv(finfo[4], lines=100)
|
||||
|
||||
|
||||
# 每2秒插入N条数据
|
||||
@repeat(every(2).seconds)
|
||||
# 每1秒插入N条数据
|
||||
@repeat(every(1).seconds)
|
||||
def inserting():
|
||||
finfo = file_info()
|
||||
if finfo[4].exists():
|
||||
print('insert...')
|
||||
logger.info('insert...')
|
||||
datas = faker_data.faker_data(lines=config['InsertRows'])[1:]
|
||||
with open(finfo[4], 'a+', encoding='utf-8', newline='') as file_csv:
|
||||
writer = csv.writer(file_csv, delimiter=',',
|
||||
quotechar='"', quoting=csv.QUOTE_ALL)
|
||||
writer.writerows(datas)
|
||||
else:
|
||||
print(str(finfo[4])+" is not exists,wating")
|
||||
logger.info(str(finfo[4])+" is not exists,wating")
|
||||
# 手动去调一下,让其整点创建
|
||||
new()
|
||||
|
||||
|
@ -98,7 +113,7 @@ def deleting_data():
|
|||
files = list(finfo[2].glob('*.csv'))
|
||||
if len(files) > 1:
|
||||
file = choice(files[:-1])
|
||||
print(str(file) + "start delete data ....")
|
||||
logger.info(str(file) + "start delete data ....")
|
||||
# 删除掉前N条数据
|
||||
with open(file, 'rb') as fr:
|
||||
data = fr.readlines()
|
||||
|
@ -108,21 +123,21 @@ def deleting_data():
|
|||
with open(file, 'wb') as fw:
|
||||
fw.writelines(new_data)
|
||||
else:
|
||||
print("file number is less 1,wait next time.")
|
||||
logger.info("file number is less 1,wait next time.")
|
||||
|
||||
|
||||
# 每隔6小时删除1个文件,低于3个不删除
|
||||
@repeat(every(6).hours)
|
||||
# @repeat(every(2).seconds)
|
||||
def deleting_file():
|
||||
print("deleting file ....")
|
||||
logger.info("deleting file ....")
|
||||
# 从data目录中随机选一个
|
||||
files = list(data_dir.rglob('*.csv'))
|
||||
if len(files) > 3:
|
||||
file = choice(files[:-1])
|
||||
file.unlink()
|
||||
else:
|
||||
print("file num is less 3, not delete. wait next time.")
|
||||
logger.info("file num is less 3, not delete. wait next time.")
|
||||
|
||||
def main():
|
||||
start_flag = True
|
||||
|
@ -130,7 +145,7 @@ def main():
|
|||
start_flag = global_var.get_value('start_flag')
|
||||
run_pending()
|
||||
time.sleep(0.3)
|
||||
print("已经停止")
|
||||
logger.info("程序已经停止")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue