_filefmt=os.path.join("logs","%Y-%m-%d.log") class MyLoggerHandler(logging.Handler): def __init__(self,filefmt=None): self.filefmt=filefmt if filefmt is None: self.filefmt=_filefmt logging.Handler.__init__(self) def emit(self,record): msg=self.format(record) _filePath=datetime.datetime.Now().strftime(self.filefmt) _dir=os.path.dirname(_filePath) try: if os.path.exists(_dir) is False: os.makedirs(_dir) except Exception: print("can not make dirs") print("filepath is "+_filePath) pass try: _fobj=open(_filePath,‘a‘) _fobj.write(msg) _fobj.write("\n") _fobj.flush() _fobj.close() except Exception: print("can not write to file") print("filepath is "+_filePath) pass if __name__ == ‘__main__‘: logging.basicConfig() logger = logging.getLogger("logger") logger.setLevel(logging.INFO) filehandler = MyLoggerHandler() logger.addHandler(filehandler) logger.info(‘log...‘)
总结 以上是内存溢出为你收集整理的python自定义logger handler全部内容,希望文章能够帮你解决python自定义logger handler所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)