[英]Azure Functions Logging - Log info at module level
I'm trying to log info to Application Insights at the module level of code as oppsosed to the function level.我正在尝试在代码的模块级别将信息记录到 Application Insights,与function级别相反。
I can successfully log INFO, WARNING etc when the logger is called from within a function (in any module of my project), but not when called outside a function, (eg initializing a module, want to log some settings)当从 function(在我项目的任何模块中)中调用记录器时,我可以成功记录 INFO、WARNING 等,但在 function 之外调用时则不能(例如初始化模块,想要记录一些设置)
For example, when running my HttpTrigger app in azure functions, this works, and logs info to app insights:例如,当在 azure 函数中运行我的 HttpTrigger 应用程序时,它可以工作,并将信息记录到应用程序洞察力:
import logging
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('this message is logged successfully')
do_something()
while this does not work:虽然这不起作用:
import logging
logging.info('this message isnt logged anywhere')
def main(req: func.HttpRequest) -> func.HttpResponse:
do_something()
I've tried using named loggers, changing logging settings eg:我尝试使用命名记录器,更改记录设置,例如:
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()
and changing config in host.json:并更改 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"
}
}
}
When running code locally, info is printed to stdout correctly everywhere.在本地运行代码时,信息会在任何地方正确打印到标准输出。
I assume I must be misunderstanding exactlty how logging works in az.我想我一定是误解了日志在 az 中的工作原理。 If anyone can fill in my gaps here for why the logging isn't working as I expect that would be appreciated!
如果有人可以在这里填补我的空白,说明为什么日志记录无法按我的预期工作,那将不胜感激!
This is the cause:这是原因:
Problem comes from the app logging is not open by default on azure.问题来自 azure 默认情况下未打开应用程序日志记录。 This is the solution:
这是解决方案:
Go to Platform features -> All Settings. Go 到平台功能 -> 所有设置。
Go to search app service logs -> app logging -> set time for saving log -> save the edit. Go 搜索应用服务日志 -> 应用日志记录 -> 设置保存日志的时间 -> 保存编辑。
Then everything will be ok.那么一切都会好起来的。
This is my code:这是我的代码:
import logging
import azure.functions as func
logger = logging.getLogger('name')
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
)
Before setting, I face the same problem as yours.在设置之前,我面临与您相同的问题。 But after setting, I can get the INFO:
但设置后,我可以得到信息:
This is the offcial doc:这是官方文档:
https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.