opencensus Azure 处理程序在 Azure 函数应用程序上创建重复日志

问题描述

我想做的是尝试在 Azure 函数应用中使用 opencensus-azure 处理程序。

环境:

  • Win10
  • python 3.7

这是示例代码

import logging
import os

import azure.functions as func
from opencensus.ext.azure.log_exporter import AzureLogHandler

log_extra = {
    'custom_dimensions': {
        'InvocationId': 1234321,}
}

logger = logging.getLogger(__name__)
if logger.hasHandlers():
    logger.handlers.clear()

# Todo: replace the all-zero GUID with your instrumentation key.
logger.addHandler(AzureLogHandler(
    connection_string=f'InstrumentationKey={os.environ.get("APPINSIGHTS_INSTRUMENTATIONKEY",None)}')
)
print(123)


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

    print(logger.handlers)

    logger.debug('debug msg',extra=log_extra)
    logger.info('info msg',extra=log_extra)
    logger.warning('warning msg',extra=log_extra)
    logger.error('error msg',extra=log_extra)
    logger.critical('critical msg',extra=log_extra)
    logger.fatal('fatal msg',extra=log_extra)
    return func.HttpResponse("received",status_code=200)

在我触发该函数后,我检查了有关应用程序洞察的结果:

enter image description here

还有我的终端截图:

enter image description here

以下几点让我感到困惑:

  • 日志级别高于“DEBUG”的日志消息被重复 3次。否则,2 次。
  • 全局作用域中的print()也是 重复2次

此外,我尝试检查日志处理程序。只有 1 个处理程序。

[<AzureLogHandler (NOTSET)>]


接下来,我将代码部署到 Azure 函数应用。以下是日志:

enter image description here

需要注意的几点:

  • 日志级别高于“DEBUG”的日志消息被重复 2次。否则,不显示
  • 全局中的 print() 仅显示 1 次(如预期)

深入了解细节,检查具有相同日志消息的日志条目的 customDimension。

这个显示了一些自动生成内容

enter image description here

这在我的代码显示log_extra

enter image description here


有关如何删除所有重复项的任何建议?

造成这些情况的原因是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)