python logging一个通用的使用模板

1 import os 2 import logbook 3 from logbook.more import ColorizedStderrHandler 4 from functools import wraps 5 check_path=\'.\' 6 LOG_DIR = os.path.join(check_path, \'log\') 7 file_stream = False 8 if not os.path.exists(LOG_DIR): 9 os.makedirs(LOG_DIR) 10 file_stream = True 11 def get_logger(name=\'jiekou\', file_log=file_stream, level=\'\'): 12 """ get logger Factory function """ 13 logbook.set_datetime_format(\'local\') 14 15 ColorizedStderrHandler(bubble=False, level=level).push_thread() 16 logbook.TimedRotatingFileHandler( 17 os.path.join(LOG_DIR, \'%s.log\' % name), 18 date_format=\'%Y-%m-%d-%H\', bubble=True, encoding=\'utf-8\').push_thread() 19 return logbook.Logger(name) 20 21 LOG = get_logger(file_log=file_stream, level=\'INFO\') 22 def logger(param): 23 """ fcuntion from logger meta """ 24 def wrap(function): 25 """ logger wrapper """ 26 @wraps(function) 27 def _wrap(*args, **kwargs): 28 """ wrap tool """ 29 LOG.info("当前模块 {}".format(param)) 30 # LOG.info("全部args参数参数信息 , {}".format(str(args))) 31 # LOG.info("全部kwargs参数信息 , {}".format(str(kwargs))) 32 return function(*args, **kwargs) 33 return _wrap 34 return wrap

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgfdds.html