123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #!/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='search'):
- '''
- 指定保存日志的文件路径,日志级别,以及调用文件
- 将日志存入到指定的文件中
- '''
- # 创建一个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_log2.log') # 日志地址
- # 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
- # testing
- class TestingCfg:
- internal_ip = '192.168.1.140' # internal
- external_ip = '192.168.1.140' # external
- server_port = 8888
- wordbin_exe = r"F:\word_bin\ConsoleApplication1.exe"
- upload_folder = r'F:\zwj\word_upload_v1'
- # production
- class ProductionCfg:
- internal_ip = '0.0.0.0' # internal
- external_ip = '49.233.23.58' if sys.argv[-1] == '58' else '49.233.14.55' # external
- server_port = 11088
- wordbin_exe = r"D:\word_bin\ConsoleApplication1.exe"
- upload_folder = r'D:\zwj\word_uploads'
- # 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.external_ip
- server_port = config_class.server_port
- # 再解析新的文件路径
- IMG_FOLDER = "F:/zwj/parse_2021/img_folder"
- new_img_ip = "http://192.168.1.140:11066/ser_static"
|