import logging import os import time def logger(user_name): "日志功能模块" logger = logging.getLogger(user_name) logger.setLevel(logging.DEBUG) log_file = ‘/tmp/tj‘ now_minute = time.strftime("%Y%m%d%H%M",time.localtime()) ago_minute = int(today)-1 fn=logging.FileHandler(log_file+‘_‘+user_name+‘_‘+today+‘.log‘) fn.setLevel(logging.DEBUG) formatter = logging.Formatter(‘%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s‘) fn.setFormatter(formatter) logger.addHandler(fn) try: os.system(‘rm -rf %s_%s_%s.log‘%(log_file,user_name,ago_minute)) except: pass return logger #自定义一个异常处理,出现异常就记录 try: a!=a except: a=logger() a.exception("记录异常")
原文:http://leizhu.blog.51cto.com/3758740/1681925