首页 > 编程知识 正文

日志模板,python日志模块

时间:2023-05-03 06:58:15 阅读:246725 作者:1324

鉴于写Python 时,经常记录实时的日志,因此需要一个模板作为工具,这里摘出了实际工作中的一部分代码作为参考

 

首先要引入必备的logging库

import os,sysimport timeimport loggingimport logging.handlerssys.dont_write_bytecode = True

定义一个日志的模板类,其中有关于日志大小的限制,这里设置最大为10M,采用滚动存储

 

class Log(object):def __init__(self, logPath, logFile):#create loggerself.logger = logging.getLogger("GantrySWStart")self.logger.setLevel(logging.INFO)#create file handlerfileHandler = logging.handlers.RotatingFileHandler(logPath+"\"+logFile+".log",mode='a', maxBytes=10*1024*1024, backupCount=2)#create formatterformatter=logging.Formatter('%(asctime)s %(name)s %(levelname)-8s %(message)s')fileHandler.setFormatter(formatter)#add file handler to loggerself.logger.addHandler(fileHandler)def __del__(self):pass #TODO:def debug(self, msg):self.logger.debug(msg)def info(self, msg):self.logger.info(msg)def warn(self, msg):self.logger.warning(msg)def error(self, msg):self.logger.error(msg)def critical(self, msg):self.logger.critical(msg)##--------------------------------------------------------------------------------------------------------------##

然后定义一个新的工作类,这里示例如下:

class AutoPowerThePSC(object):def __init__(self,log):self.log = logpass

 

 

然后可以测试一下日志的运行情况

if __name__=='__main__':logPath = "D:UIHlog" logFile = "AutoPowerOff.log"myLog = Log(logPath,logFile) ObjectPSC = AutoPowerThePSC(myLog)

 

OK,done !

 

 

主要概念 文件存储 UFS历史中提交的图片或压缩文件

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。