#!/usr/bin/env/python # -*- coding:utf-8 -*- import logging import time import os import sys import datetime import logging.handlers class myLog(object): """ 封装后的logging """ def __init__(self, logger=None, log_cate='my_log'): """ 指定保存日志的文件路径,日志级别,以及调用文件 将日志存入到指定的文件中 :param logger: :param log_cate: 日志名 """ # 创建一个logger self.logger = logging.getLogger(logger) self.logger.setLevel(logging.INFO) # DEBUG # 创建一个handler,用于写入日志文件 # self.log_time = time.strftime("%Y_%m_%d") # file_dir = os.getcwd() + '/../log' # if not os.path.exists(file_dir): # os.mkdir(file_dir) # self.log_path = file_dir # self.log_name = self.log_path + "/" + log_cate + "." + self.log_time + '.log' # self.log_name = os.path.join(log_dir, 'parse_log.log') # 日志地址 self.log_name = os.path.join(parse_log_dir, '{}.log'.format(log_cate)) # 日志地址 # if os.path.exists(self.log_name): # 设置日志定长自动新建 # logsize = os.path.getsize(self.log_name) # if logsize > 180000000: # 180M # os.rename(self.log_name, os.path.join(parse_log_dir, '{}_{}.log'.format(log_cate, # datetime.datetime.now().strftime('%m_%d')))) # fh = logging.FileHandler(self.log_name, mode='a', encoding='utf-8', delay=True) fh = logging.handlers.RotatingFileHandler(self.log_name, maxBytes=150000000, backupCount=3, mode='a', encoding='utf-8', delay=True) fh.setLevel(logging.INFO) # 再创建一个handler,用于输出到控制台 # ch = logging.StreamHandler() # ch.setLevel(logging.INFO) # 定义handler的输出格式 # formatter = logging.Formatter( # '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s') # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') formatter = logging.Formatter('{"host-ip": ' + '"{}"'.format(external_ip) + ', "log-msg": %(message)s, "other-msg": "%(filename)s-%(lineno)s-%(asctime)s"}') fh.setFormatter(formatter) # ch.setFormatter(formatter) # 给logger添加handler self.logger.addHandler(fh) # self.logger.addHandler(ch) # 添加下面一句,在记录日志之后移除句柄 # self.logger.removeHandler(ch) # self.logger.removeHandler(fh) # 关闭打开的文件 fh.close() # ch.close() def getlog(self): return self.logger class TestingCfg140: # testing internal_ip = '192.168.1.140' # internal external_ip = '192.168.1.140' # external server_port = 11068 public_bucket = 'zxhx-1302712961' # 桶名称 region = "ap-shanghai" # 存储桶地域 public_bucket_addr = 'zxhx-1302712961.cos.ap-shanghai.myqcloud.com' FAIL_FOLDER = "F:/zwj/Text_Structure/fail_files" IMG_FOLDER = "F:/zwj/Text_Structure/img_folder" RES_FOLDER = "F:/zwj/Text_Structure/res_folder" parse_log_dir = "F:/zwj/Text_Structure/logs_art" accept_files_dir = "F:/zwj/Text_Structure/accept_files" raw_img_upload_folder = 'F:/zwj/word_folder' old_img_ip = "http://192.168.1.140:8800/ser_static" mathpix_ip = "http://192.168.1.208:8001/segment/formula/" kps_phy_ip = "http://192.168.1.86:11088/phy_mark_and_connect" repeat_ip = "http://192.168.1.192:8866/api/repeat/subject" # callback_url_taskcheck = "http://zsytk3api.testing.xueping.com/v1/interior-api/record" callback_url_taskcheck = "http://zsytk3api.dev.xueping.com/v1/interior-api/record" class TestingCfg: # testing internal_ip = '192.168.1.65' # internal external_ip = '192.168.1.65' # external server_port = 11068 public_bucket = 'zxhx-1302712961' # 桶名称 region = "ap-shanghai" # 存储桶地域 public_bucket_addr = 'zxhx-1302712961.cos.ap-shanghai.myqcloud.com' FAIL_FOLDER = "G:/zwj/WL/Text_Structure/fail_files" IMG_FOLDER = "G:/zwj/WL/Text_Structure/img_folder" RES_FOLDER = "G:/zwj/WL/Text_Structure/res_folder" parse_log_dir = "G:/zwj/WL/Text_Structure/logs_art" accept_files_dir = "G:/zwj/WL/Text_Structure/accept_files" raw_img_upload_folder = 'G:/zwj/WL/word_folder' old_img_ip = "http://192.168.1.140:8800/ser_static" mathpix_ip = "http://192.168.1.208:8001/segment/formula/" kps_phy_ip = "http://192.168.1.86:11088/phy_mark_and_connect" repeat_ip = "http://192.168.1.192:8866/api/repeat/subject" # callback_url_taskcheck = "http://zsytk3api.testing.xueping.com/v1/interior-api/record" callback_url_taskcheck = "http://zsytk3api.dev.xueping.com/v1/interior-api/record" class ProductionCfg: # production internal_ip = '0.0.0.0' # internal external_ip = '82.156.255.225' # external server_port = 11033 public_bucket = 'zxhx-pro-1302712961' # 桶名称 region = "ap-beijing" # 存储桶地域 public_bucket_addr = 'zxhx-pro-1302712961.cos.ap-beijing.myqcloud.com' # 桶地址 FAIL_FOLDER = "E:/Text_Structure/fail_files" IMG_FOLDER = "E:/Text_Structure/img_folder" RES_FOLDER = "E:/Text_Structure/res_folder" parse_log_dir = "E:/Text_Structure/logs_art" accept_files_dir = "E:/Text_Structure/accept_files" raw_img_upload_folder = 'E:/new_word_parse_2021/word_uploads' old_img_ip = "http://82.156.255.225:11086/ser_static" mathpix_ip = "http://10.19.1.11:7080/segment/formula/" kps_phy_ip = "http:/49.232.72.198:11088/phy_mark_and_connect" repeat_ip = "http://10.19.1.18:8866/api/repeat/subject" callback_url_taskcheck = "http://api.tk.zhixinhuixue.com/v1/interior-api/record" class ProductionCfg175: # production internal_ip = '0.0.0.0' # internal external_ip = '82.156.91.175' # external server_port = 11088 public_bucket = 'zxhx-pro-1302712961' # 桶名称 region = "ap-beijing" # 存储桶地域 public_bucket_addr = 'zxhx-pro-1302712961.cos.ap-beijing.myqcloud.com' # 桶地址 FAIL_FOLDER = "E:/zwj/Text_Structure/fail_files" IMG_FOLDER = "E:/zwj/Text_Structure/img_folder" RES_FOLDER = "E:/zwj/Text_Structure/res_folder" parse_log_dir = "E:/zwj/Text_Structure/logs" accept_files_dir = "E:/zwj/Text_Structure/accept_files" raw_img_upload_folder = 'E:/zwj/new_word_parse_2021/word_uploads' old_img_ip = "http://82.156.255.225:11086/ser_static" mathpix_ip = "http://10.19.1.11:7080/segment/formula/" kps_phy_ip = "http:/49.232.72.198:11088/phy_mark_and_connect" repeat_ip = "http://10.19.1.18:8866/api/repeat/subject" callback_url_taskcheck = "http://api.tk.zhixinhuixue.com/v1/interior-api/record" # sys.argv:从控制台窗台运行程序,程序后加参数,以空格隔开,sys.argv[0]即程序本身 config_class = TestingCfg140 # 没有参数时,默认按测试环境 if len(sys.argv) > 1: print(sys.argv, sys.argv[0]) if sys.argv[1] == 'test': config_class = TestingCfg elif sys.argv[1] == 'product': config_class = ProductionCfg if sys.argv[-1] == '175': config_class = ProductionCfg175 else: print('cmd should be: python server.py test') print('or: python server.py product') raise ValueError("命令不正确") server_ip = config_class.internal_ip external_ip = config_class.external_ip server_port = config_class.server_port public_bucket_addr = config_class.public_bucket_addr public_bucket = config_class.public_bucket region = config_class.region parse_log_dir = config_class.parse_log_dir if not os.path.isdir(parse_log_dir): os.makedirs(parse_log_dir) # 定义结构化失败文件保存路径 FAIL_FOLDER = config_class.FAIL_FOLDER if not os.path.isdir(FAIL_FOLDER): os.makedirs(FAIL_FOLDER) # 定义再结构化文件中新图片(base64)的保存路径及访问目录 IMG_FOLDER = config_class.IMG_FOLDER if not os.path.isdir(IMG_FOLDER): os.makedirs(IMG_FOLDER) # 回调结果保存路径 RES_FOLDER = config_class.RES_FOLDER if not os.path.isdir(RES_FOLDER): os.makedirs(RES_FOLDER) # 接收文件保存路径 accept_files_dir = config_class.accept_files_dir if not os.path.isdir(accept_files_dir): os.makedirs(accept_files_dir) new_img_ip = "http://{0}:{1}/ser_static".format(external_ip, server_port) old_img_ip = config_class.old_img_ip RawImg_UploadFolder = config_class.raw_img_upload_folder mathpix_ip = config_class.mathpix_ip callback_url_taskcheck = config_class.callback_url_taskcheck kps_phy_ip = config_class.kps_phy_ip repeat_ip = config_class.repeat_ip # 作答类型 answer_type = {"选择题": 1, "填空题": 2, "解答题": 3, "判断题": 4, "英语作文": 5, "语文作文": 6}