Python 3.x 如何自定义Python日志记录的消息格式?

Python 3.x 如何自定义Python日志记录的消息格式?,python-3.x,logging,Python 3.x,Logging,我正在为我的程序设置一个记录器 custom_logger = logging.getLogger(__name__) custom_logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s;%(message)s;%(filename)s;%(lineno)d',"%Y-%m-%d %H:%M:%S") file_handler = logging.FileHandler('beispiel.log')

我正在为我的程序设置一个记录器

custom_logger = logging.getLogger(__name__)
custom_logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s;%(message)s;%(filename)s;%(lineno)d',"%Y-%m-%d %H:%M:%S")
file_handler = logging.FileHandler('beispiel.log')
file_handler.setFormatter(formatter)
custom_logger.addHandler(file_handler)


我想记录这部分
custom_logger.info(z1serial.isOpen())

它会向日志文件中添加一个TRUE
2020-03-03 13:47:38;是的;test.py;55


如何插入特定消息,如“设备已连接”;是的,所以它会出现在日志文件中?

要在消息中插入参数,它需要采用如下格式字符串

custom_logger.debug('连接的设备:%s',z1serial.isOpen())

没什么特别的