python记录程序输出及错误信息(log,logging)

import logging

# 配置logging (filemode:a+为追加log,设置为w+则表示每次清空,重新记录log)
logging.basicConfig(level=logging.DEBUG, filename="logfile", filemode="a+",
                        format="%(asctime)-15s %(levelname)-8s %(message)s")  
logging.info('\n*********************** New logging start... **************************\n  ')                      

def test():     
    try:
        something
    except Exception as e:
        # print("error:{}:{}".format(lib_name, possible_version), e)
        logging.error("open zip file error: {}".format(lib_name))  # 记录异常
        continue

if __name__ == '__main__':
	test()
	logging.info('work done: {}.'.format(work_id))  # 记录输出


log的内容为:
2012-10-18 06:40:03,582 ERROR    open zip file error:... 
2012-10-18 06:40:03,582 INFO     hello

Python日志库logging总结

你可能感兴趣的:(python记录程序输出及错误信息(log,logging))