pytest日志

class BSLogger:
    def __init__(self, name):
        # 实例化一个logger。getLogger如果不传name,那么默认就是root。如果传了name,那就是自己的logger
        self.logger = logging.getLogger(name)
        # 在实例化logger上设置一个大级别,这个大级别需要低一点,便于后面渠道开关好设置高点
        self.logger.setLevel(bs_config.cp.get("logging", "level"))

        message_format = '%(asctime)s %(name)s %(filename)s %(funcName)s [line:%(lineno)d] %(levelname)s %(message)s'
        date_format = '%a, %d %b %Y %H:%M:%S'
        formatter = logging.Formatter(fmt=message_format, datefmt=date_format)

        # 设置 console渠道
        console_handler = logging.StreamHandler()
        console_handler.setLevel(bs_config.cp.get("logging", "level"))
        console_handler.setFormatter(formatter)
        # 将 console渠道 绑定到实例化的logger上
        self.logger.addHandler(console_handler)

        # 获取日志写文件开关
        if bs_config.cp.get("log", "switch") == "on":
            # 设置 文件渠道
            file_handler = RotatingFileHandler(root_logger_path, maxBytes=bs_config.cp.getint("logging", "max_byte"), 
                                        backupCount=bs_config.cp.getint("logging", "backup_count"), encoding="utf-8")
            file_handler.setLevel("INFO")
            file_handler.setFormatter(fmt=formatter)
            self.logger.addHandler(file_handler)

bs_logger = BSLogger("api").logger

你可能感兴趣的:(pytest日志)