123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #!/usr/bin/env/python
- # -*- coding:utf-8 -*-
- import logging
- import time
- import os
- import sys
- # 日志路径
- # log_dir = os.getcwd() + '\logs'
- # if not os.path.exists(log_dir):
- # os.mkdir(log_dir)
- class myLog(object):
- '''
- 封装后的logging
- '''
- def __init__(self, logger=None, log_cate='my_log'):
- '''
- 指定保存日志的文件路径,日志级别,以及调用文件
- 将日志存入到指定的文件中
- '''
- # 创建一个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)) # 日志地址
- # print(self.log_name)
- # fh = logging.FileHandler(self.log_name, 'a') # 追加模式 这个是python2的
- fh = logging.FileHandler(self.log_name, mode='a', encoding='utf-8', delay=True)
- # fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8') # 这个是python3的
- 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')
- 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 TestingCfg: # testing
- internal_ip = '192.168.1.140' # internal
- external_ip = '192.168.1.140' # external
- server_port = 11066
- 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"
- 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/"
- class ProductionCfg: # production
- internal_ip = '0.0.0.0' # internal
- external_ip = '82.156.255.225' # 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:/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"
- 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/"
- # sys.argv:从控制台窗台运行程序,程序后加参数,以空格隔开,sys.argv[0]即程序本身
- config_class = TestingCfg # 没有参数时,默认按测试环境
- if len(sys.argv) > 1:
- print(sys.argv, sys.argv[0])
- print(sys.argv[1])
- if sys.argv[1] == 'test':
- config_class = TestingCfg
- elif sys.argv[1] == 'product':
- config_class = ProductionCfg
- 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
- # 定义结构化失败文件保存路径
- 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)
- 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
|