如何查看 Python Azure Function App 的错误日志?

huangapple go评论69阅读模式
英文:

How can I view error logs from a Python Azure Function App?

问题

我正在使用Linux和消耗计划创建一些Python函数应用程序。我试图找出在哪里可以查看我的应用程序的错误日志,但没有太多成功。特别是我想查看两个事情:

  • 如果我的应用程序代码编译失败,我尝试访问API端点时会收到404错误。我在哪里看到编译错误?

  • 如果我的应用程序引发异常,我会从端点收到500错误。我在哪里看到异常消息和回溯?

我在找到的所有日志和洞察屏幕中都没有找到这些信息。我不得不将所有的代码都包装在try/except块中,并在响应中返回错误,但这并不理想。我肯定漏掉了一些明显的东西!

感谢帮助。

英文:

I'm working on some Python Function Apps using Linux and the consumption plan. I'm trying to figure out where I can see error logs from my apps without a lot of success. In particular I want to see two things:

  • If my app code fails to compile I get 404 errors when I try to access the API endpoint. Where do I see the compile error?

  • If my app throws an exception I get 500 errors from the endpoint. Where do I see the exception message and traceback?

Neither of these appear in any of the logging or insights screens I've been able to find. I've resorted to wrapping all my code in try/except blocks and returning the errors in my response but that's not great. I must be missing something obvious here!

Thanks for the help.

答案1

得分: 1

以下是翻译好的部分:

有许多方法可以监视您的Azure Functions触发器,如下所示:

我从本地机器部署了一个HTTP触发器函数到Azure门户,并触发它以获取日志,如下所示:

方法1) 转到Azure函数应用程序 > 选择您的触发器 > 代码 + 测试 > 测试函数,日志流将记录所有触发器的失败和成功以及错误代码,如下所示:

如何查看 Python Azure Function App 的错误日志?

然后,您可以将日志类型更改为Verbose(详细信息)、Information(信息)、Warning(警告)、Error(错误)。如果在触发HTTP请求或任何函数触发API后上述日志未出现,只需更改日志记录类型。

如何查看 Python Azure Function App 的错误日志?

方法2) 访问监视部分 > 并检查所有触发器请求调用、警告和错误,如下所示:

如何查看 Python Azure Function App 的错误日志?

如何查看 Python Azure Function App 的错误日志?

您可以单击在Application Insights中运行查询并查询日志:

如何查看 Python Azure Function App 的错误日志?

日志:

如何查看 Python Azure Function App 的错误日志?

方法3)

您可以访问函数应用程序并选择日志选项卡,然后查询日志,如下所示:

如何查看 Python Azure Function App 的错误日志?

您可以访问日志流并找到与您的函数的相同控制台日志。当您的函数应用程序中有多个触发器时,这是一个快速检查函数触发器日志的单一点。

如何查看 Python Azure Function App 的错误日志?

您可以单击“指标”以获取有关函数的失败请求和其他参数的见解,如下所示:

如何查看 Python Azure Function App 的错误日志?

参考我的SO线程答案以在Azure Function中使用Application Insights发送自定义日志。

您还可以根据此MS文档中的参考,在host.json中添加特定的日志参数以配置Azure Functions日志记录。

{   "logger": {
    "categoryFilter": {
      "defaultLevel": "Information",
      "categoryLevels": {
        "Host": "Error",
        "Function": "Error",
        "Host.Aggregator": "Information"
      }
    }  
  }
}

在Azure函数应用程序的消耗基础计划中,已禁用了App Service日志。如果您想要检查App Service日志和高级工具kudu,这些工具还包含有关函数部署和其他日志的详细信息,您需要将函数应用程序创建为高级计划。请参考以下内容:

函数应用程序 > 开发工具 > 高级工具 > Kudu > 进入

如何查看 Python Azure Function App 的错误日志?

启用App Service日志:

如何查看 Python Azure Function App 的错误日志?

参考:

监视Azure Functions | Microsoft Learn

英文:

There are many ways you can monitor your Azure Functions Trigger, Refer below:-

I deployed one HTTP trigger function on Azure Portal from my local machine and triggered it to get the logs like below:-

Method 1) Go to your Azure Function app > Select your trigger > Code + Test > Test the Function and the Log stream will log all your Trigger's failure and success with error code, Refer below:-

如何查看 Python Azure Function App 的错误日志?

And then you can change the logging type to either Verbose, Information, Warning, Error. If the above logs did not appear after you trigger your HTTP request or any function Trigger API, Just change the Logging to another type.

如何查看 Python Azure Function App 的错误日志?

Method 2) Visit Monitor Section > and check for all the Trigger request invocations, warnings, errors like below:-

如何查看 Python Azure Function App 的错误日志?

如何查看 Python Azure Function App 的错误日志?

You can click on run query in Application insights and query the logs:-

如何查看 Python Azure Function App 的错误日志?

Logs:-

如何查看 Python Azure Function App 的错误日志?

Method 3)

You can visit your function app and select Logs tab and query Logs like below:-

如何查看 Python Azure Function App 的错误日志?

You can visit Log Stream and find the same console Logs for your Function, This option is feasible when you have multiple Triggers inside one function app, Where it gives a single point to check the Function Trigger logs quickly:-

如何查看 Python Azure Function App 的错误日志?

You can click on Metrics and get insights on your function's failed requests and other parameter like below:-

如何查看 Python Azure Function App 的错误日志?

Refer my SO thread answer to send custom logs in Azure Function with Application Insights.

You can also add specific logging parameters in your host.json as referred from this MS Document on configuring Azure Functions logging.

> json
>
> { "logger": {
> "categoryFilter": {
> "defaultLevel": "Information",
> "categoryLevels": {
> "Host": "Error",
> "Function": "Error",
> "Host.Aggregator": "Information"
> }
> }
> }
> }
>
>

App service Logs are disabled in Azure Function app with Consumption based plan, If you want to check App Service Logs and Advance tool kudu which also has details of your Function deployment and other Logs, You need to create your function app as Premium plan. Refer below:-

Function app > Development Tools > Advance Tools > Kudu > Go

如何查看 Python Azure Function App 的错误日志?

Enable App Service logs:-

如何查看 Python Azure Function App 的错误日志?

Reference:-

Monitoring Azure Functions | Microsoft Learn

huangapple
  • 本文由 发表于 2023年6月12日 03:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定