赞
踩
#以下内容放在所有代码之前,实现print自动打印到日志 import os,sys,time,io import builtins as __builtin__ def print(*args, **kwargs): # __builtin__.print('New print function') return __builtin__.print(time.strftime("%Y-%m-%d %H:%M:%S ----- ", time.localtime()) ,*args, **kwargs) class Logger(object): def __init__(self, filename="Default.log", path="./"): sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') self.terminal = sys.stdout self.log = open(os.path.join(path, filename), "a", encoding='utf8') def write(self, message): self.terminal.write(message) self.log.write(message) def flush(self): pass sys.stdout = Logger('all.log', path="") #以上内容放在所有代码之前,实现print自动打印到日志
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。