import osimport loggingimport timeimport tracebackimport re#os.listdir删除文件def rmFile(path): files = os.listdir(path) for file in files: file_path = os.path.join(path, file) if os.path.isfile(file_path): try: os.remove(file_path) except: print("except: 删除%s" % file_path)#日志输出部分def comnLogger(logfile): handler = logging.FileHandler(logfile, mode="w", encoding="utf-8") myformat = "%(asctime)s %(filename)s [line:%(lineno)d]:%(levelname)s %(message)s" formatter = logging.Formatter(myformat) handler.setFormatter(formatter) mylogger = logging.getLogger("logs") mylogger.addHandler(handler) mylogger.setLevel(logging.DEBUG) return mylogger#创建日志存放目录logdir = r"C:\Users\shtang\Desktop\xingBaKeSync" if not os.path.isdir(logdir): os.makedirs(logdir)#清空logdir下的文件rmFile(logdir)#定义日志输出文件logname = "log_" + time.strftime("%Y%m%d_%H%M%S",time.localtime()) + ".txt"logfile = os.path.join(logdir,logname)#定义一个公共的loggermylogger = comnLogger(logfile)#覆写print方法def print(message): mylogger.info(message) rpa.logger.info(message)
转载于:https://www.cnblogs.com/harryTree/p/11375761.html
相关资源:Python中使用logging模块打印log日志详解