Amazon web services AWS Beanstalk-如何打印应用程序代码中的内容并在部署中查看

Amazon web services AWS Beanstalk-如何打印应用程序代码中的内容并在部署中查看,amazon-web-services,amazon-ec2,amazon-elastic-beanstalk,Amazon Web Services,Amazon Ec2,Amazon Elastic Beanstalk,我目前正在向AWS Beanstalk部署一个Flask应用程序,我正在尝试注销应用程序中的一些内容(打印),但我不确定如何在Beanstalk中查看它,请指导我,谢谢 打印应该在/var/log/web.output.log中。但是,它们可能会延迟出现。因此,我发现从flask连接到gunicorn记录器更容易,它可以实时更新web.output.log 下面是示例application.py,您可以对其进行测试并了解如何设置它: import os from flask import Fla

我目前正在向AWS Beanstalk部署一个Flask应用程序,我正在尝试注销应用程序中的一些内容(打印),但我不确定如何在Beanstalk中查看它,请指导我,谢谢

打印应该在
/var/log/web.output.log
中。但是,它们可能会延迟出现。因此,我发现从flask连接到
gunicorn
记录器更容易,它可以实时更新
web.output.log

下面是示例
application.py
,您可以对其进行测试并了解如何设置它:

import os
from flask import Flask, Blueprint
from flask_restful import Api

from datetime import datetime

import logging

application = Flask(__name__)

@application.route("/")
def hello():

    current_time = datetime.now().strftime("%H:%M:%S")

    print(f"print from the app {current_time}")
    
    application.logger.info(f"info from hello {current_time}")
    application.logger.error(f"error from hello {current_time}")
    
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == '__main__':
    application.run()
else:
    gunicorn_logger = logging.getLogger('gunicorn.error')
    application.logger.handlers = gunicorn_logger.handlers
    application.logger.setLevel(logging.INFO)
导入操作系统
从烧瓶进口烧瓶,蓝图
从restful导入Api
从日期时间导入日期时间
导入日志记录
应用=烧瓶(名称)
@应用程序。路径(“/”)
def hello():
当前时间=datetime.now().strftime(“%H:%M:%S”)
打印(f“从应用程序{current_time}打印”)
application.logger.info(f“来自hello{current_time}的信息”)
application.logger.error(f“来自hello{current_time}的错误”)
返回“你好!”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
application.run()
其他:
gunicorn\u logger=logging.getLogger('gunicorn.error')
application.logger.handlers=gunicorn\u logger.handlers
application.logger.setLevel(logging.INFO)

非常感谢您的回答!我可以问一下为什么将记录器放在else语句中吗?抱歉,我是使用elastic beanstalk的新手,也是部署的新手!另外,我可以问一下,在我的应用程序中设置gunicorn是否需要进行其他配置?或者它已经由elastic beanstalk处理了?@jason,因此它只在文件未直接执行时运行。请看一下,因为设置主要基于此。哦,我明白了!运行WSGI gunicorn时,应用程序文件不会直接执行,因此它将通过记录器运行。非常感谢你的解释!非常感谢。我可以问一下,用弹性豆茎制作gunicorn的步骤是什么?或者它会由eb自动设置,而我们根本不需要处理它?(除了你给我的登录代码)@jason在基于amazonlinux2的EB平台上,
gunicorn
是现成的。