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
使用后日志展示如图所示
[2017-10-16 20:54:46.439386] INFO: jiekou: 当前模块 requests封装 [2017-10-16 20:54:46.900412] INFO: jiekou: 当前模块 解析测试用例文件 [2017-10-16 20:54:47.323436] INFO: jiekou: 当前模块 解析测试用例文件 [2017-10-16 20:54:47.332437] INFO: jiekou: 当前模块 测试 [2017-10-16 20:54:47.554450] INFO: jiekou: inputdata> 参数:sasa, url:http://www.tuling123.com/openapi/api ,返回:{\'code\': 40001, \'text\': \'亲爱的,key不对哦。\'},预期:code=40001 [2017-10-16 20:54:47.556450] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:47.654455] INFO: jiekou: inputdata> 参数:, url:http://www.tuling123.com/openapi/api ,返回:{\'code\': 40002, \'text\': \'有啥事吗?\'},预期:code=40002 [2017-10-16 20:54:47.656455] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:48.161484] INFO: jiekou: inputdata> 参数:, url:https://api.douban.com/v2/photo/:id ,返回:{\'msg\': \'uri_not_found\', \'code\': 1001, \'request\': \'POST /v2/photo/:id\'},预期:code=1001&msg=uri_not_found [2017-10-16 20:54:48.163484] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:48.230488] INFO: jiekou: 当前模块 保存测试结果 [2017-10-16 20:54:56.272948] INFO: jiekou: 当前模块 requests封装 [2017-10-16 20:54:56.295950] INFO: jiekou: 当前模块 解析测试用例文件 [2017-10-16 20:54:56.323951] INFO: jiekou: 当前模块 解析测试用例文件 [2017-10-16 20:54:56.330952] INFO: jiekou: 当前模块 测试 [2017-10-16 20:54:56.548964] INFO: jiekou: inputdata> 参数:sasa, url:http://www.tuling123.com/openapi/api ,返回:{\'code\': 40001, \'text\': \'亲爱的,key不对哦。\'},预期:code=40001 [2017-10-16 20:54:56.548964] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:56.636969] INFO: jiekou: inputdata> 参数:, url:http://www.tuling123.com/openapi/api ,返回:{\'code\': 40002, \'text\': \'有些话,难以说出口?\'},预期:code=40002 [2017-10-16 20:54:56.638969] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:56.844981] INFO: jiekou: inputdata> 参数:, url:https://api.douban.com/v2/photo/:id ,返回:{\'msg\': \'uri_not_found\', \'code\': 1001, \'request\': \'POST /v2/photo/:id\'},预期:code=1001&msg=uri_not_found [2017-10-16 20:54:56.846981] INFO: jiekou: 当前模块 断言测试结果 [2017-10-16 20:54:56.888984] INFO: jiekou: 当前模块 保存测试结果