You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions本地运行时无法禁用SDK HTTP日志,如何解决?

问题

本地运行Azure Functions时,会输出大量不需要的HTTP请求与响应日志,示例如下:

响应日志示例

[2023-03-01T12:45:15.038Z] Response status: 200
[2023-03-01T12:45:15.038Z] Response headers:
[2023-03-01T12:45:15.038Z]     'Cache-Control': 'no-cache'
[2023-03-01T12:45:15.038Z]     'Pragma': 'no-cache'
[2023-03-01T12:45:15.038Z]     'Content-Type': 'application/json; charset=utf-8'
[2023-03-01T12:45:15.038Z]     'Expires': '-1'
[2023-03-01T12:45:15.038Z]     'x-ms-keyvault-region': 'germanywestcentral'
[2023-03-01T12:45:15.038Z]     'x-ms-client-request-id': 'xxx'
[2023-03-01T12:45:15.038Z]     'x-ms-request-id': 'xxx'
[2023-03-01T12:45:15.038Z]     'x-ms-keyvault-service-version': '1.9.713.1'
[2023-03-01T12:45:15.038Z]     'x-ms-keyvault-network-info': 'conn_type=Ipv4;addr=xxx;act_addr_fam=InterNetwork;'
[2023-03-01T12:45:15.038Z]     'X-Content-Type-Options': 'REDACTED'
[2023-03-01T12:45:15.038Z]     'Strict-Transport-Security': 'REDACTED'
[2023-03-01T12:45:15.038Z]     'Date': 'Wed, 01 Mar 2023 12:45:14 GMT'
[2023-03-01T12:45:15.038Z]     'Content-Length': '458'

请求日志示例

[2023-03-01T12:45:15.992Z] Request URL: 'https://xxx.file.core.windows.net/xxx/xxx.xlsx'
[2023-03-01T12:45:15.992Z] Request method: 'PUT'
[2023-03-01T12:45:15.992Z] Request headers:
[2023-03-01T12:45:15.992Z]     'x-ms-version': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-content-length': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-type': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-file-permission': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-file-attributes': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-file-creation-time': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'x-ms-file-last-write-time': 'REDACTED'
[2023-03-01T12:45:15.992Z]     'Accept': 'application/xml'
[2023-03-01T12:45:15.993Z]     'User-Agent': 'azsdk-python-storage-file-share/12.10.1 Python/3.9.16 (macOS-13.1-arm64-arm-64bit)'
[2023-03-01T12:45:15.993Z]     'x-ms-date': 'REDACTED'
[2023-03-01T12:45:15.993Z]     'x-ms-client-request-id': 'xxx'
[2023-03-01T12:45:15.993Z]     'Authorization': 'REDACTED'
[2023-03-01T12:45:15.993Z] No body was attached to the request
[2023-03-01T12:45:16.100Z] Response status: 201

已尝试通过客户端初始化参数禁用日志,但无效:

client = SecretClient(vault_url=KVUri, credential=credential, logging=False)
....
# upload file to share
file_client = ShareFileClient.from_connection_string(
    conn_str='xxx',
    share_name=container_name,
    file_path=file_name,
    logging_enable=False,
)

需求:保留应用内自定义日志(如logging.info('example message')),禁用上述HTTP请求/响应日志。

解决方案

这些HTTP日志来自Azure Python SDK的底层日志器,需通过Python标准logging模块调整特定日志器的级别来禁用:

  • 针对Azure Key Vault SDK:调整azure.keyvault.secrets和azure.core.pipeline.policies.http_logging_policy的日志级别至WARNING或更高
  • 针对Azure Storage SDK:调整azure.storage.fileshare和azure.core.pipeline.policies.http_logging_policy的日志级别至WARNING或更高

在Function的初始化代码(如__init__.py或函数文件开头)添加以下配置:

import logging

# 禁用Azure SDK的HTTP详细日志
logging.getLogger("azure.keyvault.secrets").setLevel(logging.WARNING)
logging.getLogger("azure.storage.fileshare").setLevel(logging.WARNING)
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.WARNING)

# 确保自定义日志正常输出(可根据需求调整级别)
logging.basicConfig(level=logging.INFO)

说明

  • 上述配置仅限制Azure SDK的HTTP请求/响应日志输出,不影响自定义的logging.info/debug等日志
  • 如果还有其他Azure服务的HTTP日志,可对应添加该服务的日志器(如azure.cosmos、azure.eventhub等)并设置级别为WARNING

内容的提问来源于stack exchange,提问作者Zin Yosrim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:45:05