标签:logger 捕获异常 print name message time 处理 handler def
捕获异常的堆栈信息
import traceback
try:
int("comeon")
except Exception as e:
print(traceback.format_exc())
日志处理
import logging
class Logger(object):
def __init__(self, file_path, level):
file_handler = logging.FileHandler(file_path, ‘a‘, encoding=‘utf-8‘)
fmt = logging.Formatter(fmt=‘%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s‘)
file_handler.setFormatter(fmt)
self.logger = logging.Logger("author:zevin", level=logging.DEBUG)
self.logger.addHandler(file_handler)
def error(self,msg):
self.logger.error(msg)
logger = Logger(‘log.txt‘,logging.DEBUG)
logger.error("nothing is ready")
标签:logger 捕获异常 print name message time 处理 handler def
原文地址:https://www.cnblogs.com/gugu-da/p/12838925.html