Azure功能记录-在模块级别记录信息

问题描述 投票:0回答:1

我正在尝试将信息记录到模块级别的代码的[[模块级别,将信息记录到Application Insights。

当从函数内部(在我的项目的任何模块中)调用记录器时,但在函数外部(例如,初始化模块,要记录某些设置时),则无法成功记录INFO,WARNING等。

例如,当在azure函数中运行HttpTrigger应用程序时,此方法有效,并将信息记录到应用程序见解中:

import logging def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('this message is logged successfully') do_something()

虽然这不起作用:

import logging logging.info('this message isnt logged anywhere') def main(req: func.HttpRequest) -> func.HttpResponse: do_something()

我尝试使用命名记录器,例如更改记录设置:

import logging log = logging.getLogger(__name__) log.setLevel(logging.INFO) log.info('This still isnt logged') def main(req: func.HttpRequest) -> func.HttpResponse: do_something()

并更改host.json中的配置:

{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[1.*, 2.0.0)" }, "logging": { "fileLoggingMode": "always", "logLevel": { "default": "Information", "Host.Results": "Information", "Function": "Information", "Host.Aggregator": "Information" } } }

在本地运行代码时,信息将在所有地方正确打印到标准输出。

我想我必须完全误解az中日志记录的工作方式。如果有人可以在这里填补我的空白,为什么我无法期望日志记录不起作用,将不胜感激!

python-3.x logging azure-functions
1个回答
0
投票
请注意您的用法是错误的。

这将起作用:

import logging import azure.functions as func logger = logging.getLogger('akshay') logger.setLevel(logging.DEBUG) sh = logging.StreamHandler() sh.setLevel(logging.INFO) logger.addHandler(sh) logger.info('This will work.') def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: return func.HttpResponse(f"Hello {name}!") else: return func.HttpResponse( "Please pass a name on the query string or in the request body", status_code=400 )

看看这个官方文档:

https://docs.python.org/3/library/logging.html#module-logging

© www.soinside.com 2019 - 2024. All rights reserved.