[英]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.