From e34d223e7dc983dd01f0f427de88242d3a5825e8 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Sun, 30 Jan 2022 17:19:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20python=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=20check=20=E5=92=8C=20mylog=20=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu/fetcher/stock/zh_stock_a_sina_qq.py | 4 +- hikyuu/gui/HikyuuTDX.py | 25 +++--- hikyuu/gui/data/CollectToMemThread.py | 18 ++-- hikyuu/gui/data/CollectToMySQLThread.py | 20 ++--- hikyuu/gui/data/ImportPytdxTimeToH5Task.py | 2 +- hikyuu/gui/data/ImportPytdxToH5Task.py | 2 +- hikyuu/gui/data/ImportPytdxTransToH5Task.py | 2 +- hikyuu/gui/data/ImportTdxToH5Task.py | 2 +- hikyuu/gui/data/ImportWeightToSqliteTask.py | 2 +- hikyuu/gui/spot_server.py | 55 +++++------- hikyuu/util/check.py | 17 +++- hikyuu/util/mylog.py | 92 ++++++++++++--------- 12 files changed, 119 insertions(+), 122 deletions(-) diff --git a/hikyuu/fetcher/stock/zh_stock_a_sina_qq.py b/hikyuu/fetcher/stock/zh_stock_a_sina_qq.py index d10f85e37..5022034e7 100644 --- a/hikyuu/fetcher/stock/zh_stock_a_sina_qq.py +++ b/hikyuu/fetcher/stock/zh_stock_a_sina_qq.py @@ -16,7 +16,7 @@ def parse_one_result_sina(resultstr): result = {} hku_check_ignore(resultstr, "Invalid input param!") - hku_check_ignore(len(resultstr) > 3 and resultstr[:3] == 'var', "Invalid input param! {}".format(resultstr)) + hku_check_ignore(len(resultstr) > 3 and resultstr[:3] == 'var', "Invalid input param! {}", resultstr) a = resultstr.split(',') result['market'] = a[0][11:13] @@ -61,7 +61,7 @@ def parse_one_result_sina(resultstr): def parse_one_result_qq(resultstr): result = {} hku_check_ignore(resultstr, "Invalid input param!") - hku_check_ignore(len(resultstr) > 3 and resultstr[:2] == 'v_', "Invalid input param! {}".format(resultstr)) + hku_check_ignore(len(resultstr) > 3 and resultstr[:2] == 'v_', "Invalid input param! {}", resultstr) a = resultstr.split('~') result['market'] = a[0][2:4] diff --git a/hikyuu/gui/HikyuuTDX.py b/hikyuu/gui/HikyuuTDX.py index 8809e927b..012864deb 100644 --- a/hikyuu/gui/HikyuuTDX.py +++ b/hikyuu/gui/HikyuuTDX.py @@ -206,24 +206,21 @@ def initLogger(self): self.log_handler = logging.StreamHandler(self._stream) FORMAT = logging.Formatter('%(asctime)-15s [%(levelname)s] - %(message)s [%(name)s::%(funcName)s]') self.log_handler.setFormatter(FORMAT) + add_class_logger_handler( + [ + MyMainWindow, + CollectSpotThread, #CollectToMySQLThread, CollectToMemThread, + UsePytdxImportToH5Thread, + UseTdxImportToH5Thread, + ImportTdxToH5Task, + SchedImportThread + ], + logging.INFO + ) for name in logging.Logger.manager.loggerDict.keys(): logger = logging.getLogger(name) logger.addHandler(self.log_handler) logger.setLevel(logging.DEBUG) - # add_class_logger_handler( - # self.log_handler, - # [ - # MyMainWindow, - # CollectSpotThread, #CollectToMySQLThread, CollectToMemThread, - # UsePytdxImportToH5Thread, - # UseTdxImportToH5Thread, - # ImportTdxToH5Task, - # SchedImportThread - # ], - # logging.INFO - # ) - # get_default_logger().addHandler(self.log_handler) - # get_default_logger().setLevel(logging.INFO) # 多进程日志队列 self.mp_log_q = multiprocessing.Queue() diff --git a/hikyuu/gui/data/CollectToMemThread.py b/hikyuu/gui/data/CollectToMemThread.py index 87a9f6ba3..b0f0dd458 100644 --- a/hikyuu/gui/data/CollectToMemThread.py +++ b/hikyuu/gui/data/CollectToMemThread.py @@ -25,17 +25,13 @@ def __init__(self, config, hku_config_file, market='SH'): self._phase1_start_time = Datetime( datetime.datetime.combine( datetime.date.today(), - datetime.time.fromisoformat( - (config.get('collect', 'phase1_start', fallback='09:05')) - ) + datetime.time.fromisoformat((config.get('collect', 'phase1_start', fallback='09:05'))) ) ) self._phase1_end_time = Datetime( datetime.datetime.combine( datetime.date.today(), - datetime.time.fromisoformat( - (config.get('collect', 'phase1_end', fallback='09:05')) - ) + datetime.time.fromisoformat((config.get('collect', 'phase1_end', fallback='09:05'))) ) ) self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False) @@ -60,7 +56,7 @@ def run(self): hikyuu_init(self.hku_config_file, ignore_preload=True) stk_list = self.get_stock_list() - hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", self.logger) + hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", logger=self.logger) self.mutex.tryLock() end_delta = self._phase1_end_time - self._phase1_end_time.start_of_day() @@ -77,14 +73,14 @@ def run(self): delta = self._interval * ceil((start - self._phase1_start_time) / self._interval ) - (start - self._phase1_start_time) - hku_info('{} 下次采集时间:{}'.format(self.market, start + delta), self.logger) + hku_info('{} 下次采集时间:{}', self.market, start + delta, logger=self.logger) delta = int(delta.total_milliseconds()) while self.working and not self.cond.wait(self.mutex, int(delta)): last_time = Datetime.today() + end_delta start = Datetime.now() if start >= last_time: next_time = Datetime.today() + TimeDelta(1) + start_delta - hku_info('{} 明日采集时间:{}'.format(self.market, next_time), self.logger) + hku_info('{} 明日采集时间:{}', self.market, next_time, logger=self.logger) delta = next_time - start delta = int(delta.total_milliseconds()) continue @@ -106,8 +102,8 @@ def get_stock_list(self): sm = StockManager.instance() return [ stk.market_code.lower() for stk in sm - if stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM) - and stk.valid and stk.market.lower() == self.market.lower() + if stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, + constant.STOCKTYPE_GEM) and stk.valid and stk.market.lower() == self.market.lower() ] def record_is_valid(self, record): diff --git a/hikyuu/gui/data/CollectToMySQLThread.py b/hikyuu/gui/data/CollectToMySQLThread.py index fae2a9357..cd4b8e0bf 100644 --- a/hikyuu/gui/data/CollectToMySQLThread.py +++ b/hikyuu/gui/data/CollectToMySQLThread.py @@ -44,17 +44,13 @@ def __init__(self, config, market='SH'): self._phase1_start_time = Datetime( datetime.datetime.combine( datetime.date.today(), - datetime.time.fromisoformat( - (config.get('collect', 'phase1_start', fallback='09:05')) - ) + datetime.time.fromisoformat((config.get('collect', 'phase1_start', fallback='09:05'))) ) ) self._phase1_end_time = Datetime( datetime.datetime.combine( datetime.date.today(), - datetime.time.fromisoformat( - (config.get('collect', 'phase1_end', fallback='09:05')) - ) + datetime.time.fromisoformat((config.get('collect', 'phase1_end', fallback='09:05'))) ) ) self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False) @@ -68,7 +64,7 @@ def stop(self): self.cond.wakeAll() self.wait() if self._connect is not None: - hku_trace('关闭数据库连接', self.logger) + hku_trace('关闭数据库连接', logger=self.logger) self._connect.close() def __del__(self): @@ -78,7 +74,7 @@ def __del__(self): def run(self): self.logger.info("{} 数据采集同步线程启动".format(self.market)) stk_list = self.get_stock_list() - hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", self.logger) + hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", logger=self.logger) self.mutex.tryLock() end_delta = self._phase1_end_time - self._phase1_end_time.start_of_day() @@ -95,14 +91,14 @@ def run(self): delta = self._interval * ceil((start - self._phase1_start_time) / self._interval ) - (start - self._phase1_start_time) - hku_info('{} 下次采集时间:{}'.format(self.market, start + delta), self.logger) + hku_info('{} 下次采集时间:{}', self.market, start + delta, logger=self.logger) delta = int(delta.total_milliseconds()) while self.working and not self.cond.wait(self.mutex, int(delta)): last_time = Datetime.today() + end_delta start = Datetime.now() if start >= last_time: next_time = Datetime.today() + TimeDelta(1) + start_delta - hku_info('{} 明日采集时间:{}'.format(self.market, next_time), self.logger) + hku_info('{} 明日采集时间:{}', self.market, next_time, logger=self.logger) delta = next_time - start delta = int(delta.total_milliseconds()) continue @@ -132,8 +128,8 @@ def process_one_record(self, record): table = get_table(connect, self.market, record['code'], 'day') sql = 'replace into {} (date, open, high, low, close, amount, count) \ values ({}, {}, {}, {}, {}, {}, {})'.format( - table, current_date, record['open'], record['high'], record['low'], record['close'], - record['amount'], record['volumn'] + table, current_date, record['open'], record['high'], record['low'], record['close'], record['amount'], + record['volumn'] ) cur = connect.cursor() cur.execute(sql) diff --git a/hikyuu/gui/data/ImportPytdxTimeToH5Task.py b/hikyuu/gui/data/ImportPytdxTimeToH5Task.py index 6bbb8cbaf..f62025328 100644 --- a/hikyuu/gui/data/ImportPytdxTimeToH5Task.py +++ b/hikyuu/gui/data/ImportPytdxTimeToH5Task.py @@ -53,7 +53,7 @@ def __init__(self, log_queue, queue, sqlitefile, market, quotations, ip, port, d self.max_days = int(max_days) def __call__(self): - capture_multiprocess_all_logger(self.log_queue, get_default_logger().level) + capture_multiprocess_all_logger(self.log_queue) count = 0 connect = sqlite3.connect(self.sqlitefile, timeout=1800) try: diff --git a/hikyuu/gui/data/ImportPytdxToH5Task.py b/hikyuu/gui/data/ImportPytdxToH5Task.py index acf41f127..b14dd2c40 100644 --- a/hikyuu/gui/data/ImportPytdxToH5Task.py +++ b/hikyuu/gui/data/ImportPytdxToH5Task.py @@ -55,7 +55,7 @@ def __init__(self, log_queue, queue, config, market, ktype, quotations, ip, port self.startDatetime = start_datetime def __call__(self): - capture_multiprocess_all_logger(self.log_queue, get_default_logger().level) + capture_multiprocess_all_logger(self.log_queue) if self.config.getboolean('hdf5', 'enable', fallback=True): sqlite_file = "{}/stock.db".format(self.config['hdf5']['dir']) connect = sqlite3.connect(sqlite_file, timeout=1800) diff --git a/hikyuu/gui/data/ImportPytdxTransToH5Task.py b/hikyuu/gui/data/ImportPytdxTransToH5Task.py index c551e0167..dc5976855 100644 --- a/hikyuu/gui/data/ImportPytdxTransToH5Task.py +++ b/hikyuu/gui/data/ImportPytdxTransToH5Task.py @@ -53,7 +53,7 @@ def __init__(self, log_queue, queue, sqlitefile, market, quotations, ip, port, d self.max_days = int(max_days) def __call__(self): - capture_multiprocess_all_logger(self.log_queue, get_default_logger().level) + capture_multiprocess_all_logger(self.log_queue) count = 0 connect = sqlite3.connect(self.sqlitefile, timeout=1800) try: diff --git a/hikyuu/gui/data/ImportTdxToH5Task.py b/hikyuu/gui/data/ImportTdxToH5Task.py index 561d655ec..6956f75ab 100644 --- a/hikyuu/gui/data/ImportTdxToH5Task.py +++ b/hikyuu/gui/data/ImportTdxToH5Task.py @@ -72,7 +72,7 @@ def __del__(self): pass def __call__(self): - capture_multiprocess_all_logger(self.log_queue, get_default_logger().level) + capture_multiprocess_all_logger(self.log_queue) use_hdf = False if self.config.getboolean('hdf5', 'enable', fallback=True): sqlite_file = "{}/stock.db".format(self.config['hdf5']['dir']) diff --git a/hikyuu/gui/data/ImportWeightToSqliteTask.py b/hikyuu/gui/data/ImportWeightToSqliteTask.py index 65dc2a73b..4ac0abf23 100644 --- a/hikyuu/gui/data/ImportWeightToSqliteTask.py +++ b/hikyuu/gui/data/ImportWeightToSqliteTask.py @@ -48,7 +48,7 @@ def __init__(self, log_queue, queue, config, dest_dir): self.msg_name = 'IMPORT_WEIGHT' def __call__(self): - capture_multiprocess_all_logger(self.log_queue, get_default_logger().level) + capture_multiprocess_all_logger(self.log_queue) total_count = 0 try: if self.config.getboolean('hdf5', 'enable', fallback=True): diff --git a/hikyuu/gui/spot_server.py b/hikyuu/gui/spot_server.py index 81a9495e7..6b7f4b963 100644 --- a/hikyuu/gui/spot_server.py +++ b/hikyuu/gui/spot_server.py @@ -125,10 +125,7 @@ def print_spot_list(buf): @hku_catch() def parse_phase(phase): - return [ - TimeDelta(hours=int(a[0]), minutes=int(a[1])) - for a in [x.split(':') for x in phase.split('-')] - ] + return [TimeDelta(hours=int(a[0]), minutes=int(a[1])) for a in [x.split(':') for x in phase.split('-')]] def next_delta(start_time, interval, phase1_delta, phase2_delta, ignore_weekend): @@ -165,33 +162,23 @@ def next_delta(start_time, interval, phase1_delta, phase2_delta, ignore_weekend) def collect(use_proxy, source, seconds, phase1, phase2, ignore_weekend): phase1_delta = parse_phase(phase1) - hku_error_if( - phase1_delta is None or len(phase1_delta) != 2, - "无效参数 phase1: {}".format(phase1), - callback=lambda: exit(1) - ) - hku_error_if( - phase1_delta[0] > phase1_delta[1], - "无效参数 phase1: {}, 结束时间应大于等于起始时间".format(phase1), - callback=lambda: exit(1) - ) + if phase1_delta is None or len(phase1_delta) != 2: + hku_error("无效参数 phase1: {}".format(phase1)) + exit(1) + if phase1_delta[0] > phase1_delta[1]: + hku_error("无效参数 phase1: {}, 结束时间应大于等于起始时间".format(phase1)) + exit(1) phase2_delta = parse_phase(phase2) - hku_error_if( - phase2_delta is None or len(phase2_delta) != 2, - "无效参数 phase2: {}".format(phase2), - callback=lambda: exit(1) - ) - hku_error_if( - phase2_delta[0] > phase2_delta[1], - "无效参数 phase2: {}, 结束时间应大于等于起始时间".format(phase2), - callback=lambda: exit(1) - ) - hku_error_if( - phase1_delta[1] > phase2_delta[0], - "无效参数 phase1: {}, phase2: {}, phase2 起始时间应大于等于 phase1 结束时间".format(phase1, phase2), - callback=lambda: exit(1) - ) + if phase2_delta is None or len(phase2_delta) != 2: + hku_error("无效参数 phase2: {}".format(phase2)) + exit(1) + if phase2_delta[0] > phase2_delta[1]: + hku_error("无效参数 phase2: {}, 结束时间应大于等于起始时间".format(phase2)) + exit(1) + if phase1_delta[1] > phase2_delta[0]: + hku_error("无效参数 phase1: {}, phase2: {}, phase2 起始时间应大于等于 phase1 结束时间".format(phase1, phase2)) + exit(1) hku_logger.info("采集时间段1:{}".format(phase1)) hku_logger.info("采集时间段2:{}".format(phase2)) @@ -205,8 +192,8 @@ def collect(use_proxy, source, seconds, phase1, phase2, ignore_weekend): sm = StockManager.instance() stk_list = [ - stk.market_code.lower() for stk in sm if stk.valid and stk.type in - (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM) + stk.market_code.lower() for stk in sm + if stk.valid and stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM) ] spot_topic = ':spot:' @@ -234,11 +221,7 @@ def batch_func(records): start_time = Datetime.now() pub_sock.send("{}{}".format(spot_topic, '[start spot]').encode('utf-8')) records = get_spot_parallel(stk_list, source, use_proxy, batch_func) - hku_info( - "{}:{}:{} 采集数量: {}".format( - start_time.hour, start_time.minute, start_time.second, len(records) - ) - ) + hku_info("{}:{}:{} 采集数量: {}".format(start_time.hour, start_time.minute, start_time.second, len(records))) pub_sock.send('{}{}'.format(spot_topic, '[end spot]').encode('utf-8')) delta = next_delta(start_time, seconds, phase1_delta, phase2_delta, ignore_weekend) time.sleep(delta.total_seconds()) diff --git a/hikyuu/util/check.py b/hikyuu/util/check.py index 7e2090414..688ab2ebf 100644 --- a/hikyuu/util/check.py +++ b/hikyuu/util/check.py @@ -45,11 +45,11 @@ def checkif(expression, message, excepion=None, **kwargs): raise excepion(message, **kwargs) -def hku_check(exp, msg): +def hku_check(exp, msg, *args, **kwargs): if not exp: st = traceback.extract_stack()[-2] check_exp = st._line.split(',')[0] - errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg, st.name, st.filename, st.lineno) + errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno) raise HKUCheckError(exp, errmsg) @@ -70,12 +70,21 @@ def hku_check_throw(expression, message, excepion=None, **kwargs): raise excepion(errmsg, **kwargs) -def hku_check_ignore(exp, msg=None): +def hku_check_ignore(exp, *args, **kwargs): """可忽略的检查""" if not exp: st = traceback.extract_stack()[-2] check_exp = st._line.split(',')[0] - errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg, st.name, st.filename, st.lineno) + msg = kwargs.pop("msg") if "msg" in kwargs else '' + if msg: + errmsg = "{}) {} [{}] [{}:{}]".format( + check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno + ) + elif args: + msg = args[0] + errmsg = "{}) {} [{}] [{}:{}]".format( + check_exp, msg.format(*args[1:], **kwargs), st.name, st.filename, st.lineno + ) raise HKUIngoreError(exp, errmsg) diff --git a/hikyuu/util/mylog.py b/hikyuu/util/mylog.py index 76a7f5cfa..362f8f83f 100644 --- a/hikyuu/util/mylog.py +++ b/hikyuu/util/mylog.py @@ -46,70 +46,78 @@ def class_logger(cls, enable=False): cls.logger = logger -def add_class_logger_handler(handler, class_list, level=logging.INFO): +def add_class_logger_handler(class_list, level=logging.INFO, handler=None): """为指定的类增加日志 handler,并设定级别 - :param handler: logging handler :param class_list: 类列表 :param level: 日志级别 + :param handler: logging handler """ for cls in class_list: #logger = logging.getLogger("{}.{}".format(cls.__module__, cls.__name__)) logger = logging.getLogger("{}".format(cls.__name__)) - logger.addHandler(handler) + if handler: + logger.addHandler(handler) logger.setLevel(level) -def hku_debug(msg, logger=None): +def hku_debug(msg, *args, **kwargs): st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None if logger: - logger.debug("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.debug("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.debug("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.debug("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) hku_trace = hku_debug -def hku_info(msg, logger=None): +def hku_info(msg, *args, **kwargs): st = traceback.extract_stack()[-2] - if logger: - logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger = kwargs.pop("logger") if "logger" in kwargs else None + if logger is not None: + logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) -def hku_warn(msg, logger=None): +def hku_warn(msg, *args, **kwargs): st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None if logger: - logger.warning("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.warning("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) -def hku_error(msg, logger=None): +def hku_error(msg, *args, **kwargs): st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None if logger: - logger.error("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.error("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) -def hku_fatal(msg, logger=None): +def hku_fatal(msg, *args, **kwargs): st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None if logger: - logger.critical("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.critical("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) -def hku_debug_if(exp, msg, logger=None, callback=None): +def hku_debug_if(exp, msg, *args, **kwargs): if exp: st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None + callback = kwargs.pop("callback") if "callback" in kwargs else None if logger: - logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) if callback: callback() @@ -117,46 +125,54 @@ def hku_debug_if(exp, msg, logger=None, callback=None): hku_trace_if = hku_debug_if -def hku_info_if(exp, msg, logger=None, callback=None): +def hku_info_if(exp, msg, *args, **kwargs): if exp: st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None + callback = kwargs.pop("callback") if "callback" in kwargs else None if logger: - logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.info("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.info("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) if callback: callback() -def hku_warn_if(exp, msg, logger=None, callback=None): +def hku_warn_if(exp, msg, *args, **kwargs): if exp: st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None + callback = kwargs.pop("callback") if "callback" in kwargs else None if logger: - logger.warning("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.warning("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) if callback: callback() -def hku_error_if(exp, msg, logger=None, callback=None): +def hku_error_if(exp, msg, *args, **kwargs): if exp: st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None + callback = kwargs.pop("callback") if "callback" in kwargs else None if logger: - logger.error("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.error("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) if callback: callback() -def hku_fatal_if(exp, msg, logger=None, callback=None): +def hku_fatal_if(exp, msg, *args, **kwargs): if exp: st = traceback.extract_stack()[-2] + logger = kwargs.pop("logger") if "logger" in kwargs else None + callback = kwargs.pop("callback") if "callback" in kwargs else None if logger: - logger.critical("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) else: - hku_logger.critical("{} [{}] ({}:{})".format(msg, st.name, st.filename, st.lineno)) + hku_logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno)) if callback: callback() @@ -176,17 +192,17 @@ def wrapper(*args, **kwargs): return with_trace_wrap -def capture_multiprocess_all_logger(queue, level): +def capture_multiprocess_all_logger(queue, level=None): """重设所有子进程中的 logger 输出指定的 queue,并重设level @param multiprocessing.Queue queue 指定的 mp Queue - @param level 日志输出等级 + @param level 日志输出等级, None为保持原有等级 """ if queue is None: return qh = logging.handlers.QueueHandler(queue) - level = get_default_logger().level for name in logging.Logger.manager.loggerDict.keys(): logger = logging.getLogger(name) logger.addHandler(qh) - logger.setLevel(level) \ No newline at end of file + if level is not None: + logger.setLevel(level) \ No newline at end of file