热门

最新

红包

立Flag

投票

同城

我的

发布
antineutron
Anthony.
3 年前
trueantineutron

file_handler = TimedRotatingFileHandler(
filename=log_path, when="M", interval=1, backupCount=30
)
file_handler.suffix = "%Y-%m-%d %H:%M:%S.log"
file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}:.log$")
file_handler.setFormatter(
logging.Formatter(
"[%(asctime)s] [%(process)d] [%(levelname)s] - %(module)s.%(funcName)s (%(filename)s:%(lineno)d) - %(message)s"
)
)
logger.addHandler(file_handler)
# 记录正常的 print 信息
sys.stdout = Logger(log_path)
# 记录 traceback 异常信息
sys.stderr = Logger(log_path)


return logger

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
# 将控制台的结果输出到a.log文件,可以改成a.txtsys.stdout = Logger('a.log', sys.stdout)sys.stderr = Logger('a.log_file', sys.stderr)class RotatingFileLogger: def __init__(self,log_name): self.log_name = log_name self.logger = self._setup_log() def _setup_log(self): logger = logging.getLogger(self.log_name) log_path = os.path.join("D:\\Code\\atc-client\\client\\log", self.log_name) logger.setLevel(logging.INFO) # interval 滚动周期, # when="MIDNIGHT", interval=1 表示每天0点为更新点,每天生成一个文件 # backupCount 表示日志保存个数 # 通过设置TimedRotatingFileHandler进行日志按周(W)、天(D)、时(H)、分(M)、秒(S)切割。
立即登录