Logiging模块日志级别
CRITICAL = 50FATAL = CRITICALERROR = 40WARNING = 30WARN = WARNINGINFO = 20DEBUG = 10NOTSET = 0
只能写入到一个文件,多次声明无效
import logging logging.basicConfig(
# filename=‘l1.log‘, # format=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s‘, # datefmt=‘%Y-%m-%d %H:%M:%S %p‘, # level=logging.INFO #这里定义这个值是阀值,如果超过这个数了才会写入文件中 # ) # logging.log(logging.ERROR,‘123123‘)
所以我们只能自定义
def error_log(message): #创建文件对象 file_1_1 = logging.FileHandler(‘error.log‘, ‘a+‘, encoding=‘utf-8‘) fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s") file_1_1.setFormatter(fmt) # 创建日志对象 logger1 = logging.Logger(‘error‘, level=logging.ERROR) # 日志对象和文件对象创建关系 logger1.addHandler(file_1_1) logger1.log(logging.FATAL,message) def run_log(message): file_1_1 = logging.FileHandler(‘run.log‘, ‘a+‘, encoding=‘utf-8‘) fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s") file_1_1.setFormatter(fmt) # 创建日志对象 logger1 = logging.Logger(‘run‘, level=logging.ERROR) # 日志对象和文件对象创建关系 logger1.addHandler(file_1_1) logger1.log(logging.FATAL,message)
traceback模块
详细错误信息打印
class NicPlugin(BasePlugin): def linux(self): """ 执行命令,获取资产信息 :return: """ ret = BaseResponse() try: result = self.cmd(‘nic‘) ret.data = result except Exception as e: v = traceback.format_exc() ret.status = False ret.error = v # 写入本地日志 obj = LoggerHelper.instance() obj.error_logger.log(50,v) return ret
时间: 2024-10-02 03:37:46