简体   繁体   English

Azure 应用服务上 Python 的本地日志在哪里?

[英]Where is local logs for Python on Azure App Service?

if I run locally, I can see my print() texts.如果我在本地运行,我可以看到我的print()文本。 But even after enable File System App Service Logs, I still don't see any logs, both in _docker.log or streaming log.但即使在启用文件系统应用服务日志之后,我仍然看不到任何日志,无论是在_docker.log还是流式日志中。 What is the right way to generate local logs for Python on Azure App Service?在 Azure 应用服务上为 Python 生成本地日志的正确方法是什么?

To close the loop, I ended up making a simple log method为了关闭循环,我最终制作了一个简单的日志方法

def writeLog(log: str):
    basepath = path.dirname(__file__)
    today = date.today()
    filepath = path.abspath(path.join(basepath, "..", "..", "LogFiles", "MyBot_" + today.strftime("%Y-%m-%d") + ".log"))
    f = open(filepath, "a+")
    f.write(today.strftime("%Y-%m-%d %H:%M:%S") + " " + log)
    f.write("\n")
    f.close()

If I don't close() , I won't be able to download the file.如果我不close() ,我将无法下载该文件。 Putting it in LogFiles together with other log files makes it easier to view from VS Code将其与其他日志文件一起放在LogFiles中,可以更轻松地从 VS Code 中查看

I test with a Flask web and set the WSGI_LOG follow this doc: Configure the FastCGI handler , it will create the log however the logs file only have the StdErr log, about StdOut it's always blank.我使用 Flask web 进行测试并按照以下文档设置 WSGI_LOG: 配置 FastCGI 处理程序,它将创建日志,但是日志文件只有StdErr日志,关于StdOut ,它始终为空白。

在此处输入图像描述

So I use another solution to redirect the standard output of your scripts to some log file.因此,我使用另一种解决方案将脚本的标准 output 重定向到某个日志文件。

import sys
sys.stdout = open('D:\\home\\LogFiles\\app.log', 'w')
print("test")

You can configure the above code, then publish your code and run it.您可以配置上述代码,然后发布您的代码并运行它。

在此处输入图像描述

I was encountering a similar issue.我遇到了类似的问题。 In my case the logging was inconsistent in relation to the other logs in the container.在我的情况下,日志记录与容器中的其他日志不一致。 In the end it seemed to be more of a log buffering issue, rather than something specific to Azure App Service.最后,它似乎更像是一个日志缓冲问题,而不是 Azure 应用服务特有的问题。 It was solved by setting the following environment variable in Azure App Service: Settings -> Container -> Application Settings通过在 Azure App Service 中设置以下环境变量来解决:设置 -> 容器 -> 应用程序设置

PYTHONUNBUFFERED = 1

In a docker run command you'd set it as:在 docker 运行命令中,您可以将其设置为:

--env PYTHONUNBUFFERED=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 azure 应用服务中的 juypter notebook kernel 获取日志 - Get logs from juypter notebook kernel in azure app service 是否可以将本地 python 应用程序连接到 Azure OIDC? - Is it possible to connect a local python app to Azure OIDC? azure 应用服务中的 Python ModuleNotFoundError 包 - Python ModuleNotFoundError package in azure app service 如何在Azure应用服务中运行Python shell - How to run a Python shell in azure app service 带有python脚本的Azure Web应用程序服务 - Azure web app service with python script Azure 应用服务 Python 3.9 部署失败 - Azure App Service with Python 3.9 deployment failed 来自两个 azure 应用服务的日志不会同时转储到单个应用洞察中 - logs from two azure app service not dumping in a single app insight at the same time Azure Functions :如何使用本地调试模式将日志写入 Azure Application Insights 和 Azure Functions 使用 Python? - Azure Function : How do I write my logs to Azure Application Insights using local debug mode with Azure Functions using Python? 在 python flask 应用程序上部署泡菜 model - Deployment of pickle model in a python flask app on Azure App Service 使用 celery 和 websocket 在 Azure 应用服务上部署 Python Django 应用 - Python Django App deployment on Azure app service with celery and websocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM