【Azure Service Bus】分享使用 Python Service Bus SDK 输出SDK内操作日志 - LuBu0505/My-Code GitHub Wiki
问题描述
使用Python代码消费Service Bus中的消息,默认情况 Console 中的信息都是通过 print 打印输出。
有时候需要调查更多SDK中的日志,那么如何才能让SDK输出更多的日志呢?
问题解答
方法就是引入 Logging SDK,然后再初始化 ServiceBusClient 对象时,设置logging_enable参数为True
import logging import sys from azure.servicebus.aio import ServiceBusClient
handler = logging.StreamHandler(stream=sys.stdout)
log_fmt = logging.Formatter(fmt="%(asctime)s | %(threadName)s | %(levelname)s | %(name)s | %(message)s")
handler.setFormatter(log_fmt)
logger = logging.getLogger('azure.servicebus')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
async with ServiceBusClient.from_connection_string(conn_str=NAMESPACE_CONNECTION_STR,logging_enable=True) as servicebus_client:
...
如此修改后,就能输出Service Bus SDK的详细日志,以供问题排查。
参考资料
Azure Service Bus client library for Python :https://learn.microsoft.com/en-us/python/api/overview/azure/servicebus-readme?view=azure-python#troubleshooting
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!