python aio libray日志和异步方法日志未显示在函数应用程序洞察中

时间:2021-05-22 08:20:03

标签: python python-3.x azure azure-functions python-asyncio

我正在使用 python aio 管理客户端库来创建 azure 资源,例如 azure.mgmt.eventhub.aio.EventHubManagementClient

观察

  • 虽然对应的 azure 同步库(例如 azure.mgmt.eventhub.EventHubManagementClient )在调用管理服务 API 时打印 http 日志,但异步库不打印类似的日志
  • 此外,当我使用异步方法并在异步方法中使用 python 记录器时,即使这些日志也不会打印。

示例代码

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    logging.basicConfig(level=logging.INFO)

    async def _create_or_update_eventhub_namespace_authorization_rule(self, authorization_rule_name,
                                                                  rights: AccessRights):
    await self._event_management_client.namespaces.create_or_update_authorization_rule(
        resource_group_name=EnvironmentVariables.RESOURCE_GROUP_NAME,
        namespace_name=self._env_var_obj.event_hub_name_space_name,
        authorization_rule_name=authorization_rule_name,
        parameters={
            "rights": [rights]
        }
    )

    logger.info('Provisioned EH-Namespace Rule:' + authorization_rule_name)

此处 create_or_update_authorization_rule() 和我自己的日志均未显示在 Azure 函数见解中。

1 个答案:

答案 0 :(得分:0)

当我将 azure 函数 main 函数本身定义为异步函数时,这个问题得到了解决,Azure python worker handle 管理事件循环并异步调用这个 main 方法

async def main(req: func.HttpRequest) -> func.HttpResponse:

有了这个,所有 aio 客户端日志和我自己的日志都被记录在应用洞察中。

注意: 早些时候我将上述方法定义为 non-async ,然后用于通过显式创建事件循环和使用 asgiref.async_to_sync

从 main() 调用 async mehtod