自己封装的代码,可以直接使用
from loguru import logger import os logs_dir = f"{os.path.dirname(os.path.dirname(__file__))}/log" file_name = 'ApiTestLogs' class Loggings: __instance = None logger.add(f"{logs_dir}/{file_name}.log", rotation="500MB", encoding="utf-8", enqueue=True, retention="10 days") def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = super(Loggings, cls).__new__(cls, *args, **kwargs) return cls.__instance def __init__(self): self.className = self.__class__.__name__ def info(self, msg): return logger.info(msg) def debug(self, msg): return logger.debug(msg) def warning(self, msg): return logger.warning(msg) def error(self, msg): ''' 打印错误信息 ''' return logger.error(msg) def exception(self,msg): ''' 打印异常信息方法 ''' return logger.exception(msg)