【问题标题】:Create simple flask error application to log exceptions创建简单的烧瓶错误应用程序来记录异常
【发布时间】:2020-07-28 20:03:45
【问题描述】:

我已将一些代码上传到服务器。该代码在本地运行,但是当我将其上传到服务器时,它给了我一个Internal Server Error。该网站使用wsgi运行,代码为:

    try:
        from decksite import main, APP as application
    except Exception as e:
        from shared import repo
        repo.create_issue('Error starting website', exception=e)
    
    if __name__ == '__main__':
        print('Running manually.  Is something wrong?')
        application.run(host='0.0.0.0', debug=False)

所以 try 和 except 都失败了。我想添加第二个异常并将其全部传递给一个简单的烧瓶应用程序,该应用程序会将两个异常都输出到浏览器并将它们记录到文件中。问题是我不知道如何将异常传递给error_appflask 应用程序,并且它在我设置日志配置的行中中断。这是我所做的。我只得到NoneType: None 而不是完整的异常。

import os, sys
sys.path.append("/home/myuser/public_html/flask");

try:
    from decksite import main, APP as application
except Exception as error:
    #from shared import repo
    #repo.create_issue('Error starting decksite', exception=error)

    #sys.path.insert(0, os.path.dirname(__file__))
    #from error_app import app as application

    # This is the code that goes into the error flask application
    import logging
    import traceback
    from flask import Flask, __version__
    app = Flask(__name__)
    application = app

    @app.route("/")
    def hello():
        return traceback.format_exc()
        # The next line gives Internal Server Error
        logging.basicConfig(filename='example.log', level=logging.DEBUG)
        logging.exception(error)
        return traceback.format_exc()


if __name__ == '__main__':
    print('Running manually.  Is something wrong?')
    application.run(host='0.0.0.0', debug=False)

我在服务器中没有 sudo,也无法通过 ssh 访问它,因此除非我能够记录错误,否则我将无法修复任何东西。

编辑:我几乎得到了我想要的:

.htaccess

website.wsgi

error_app.py

website/init.py

website/main.py

【问题讨论】:

    标签: python flask logging


    【解决方案1】:

    创建自定义 500 处理程序并打印出引用

    import traceback
    
    @app.errorhandler(500)
    def internal_server_error(e):
        return render_template('500_error.html', traceback=traceback.format_exc())
    

    让您的 '500_error.html' 模板向您显示回溯。

    【讨论】:

    • 我想我应该避免使用任何模板,以防万一模板目录所在的模块尚未导入。
    • 你可以使用render_template_string(...)或者直接返回json格式。
    • 除了之前的错误,我得到了Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
    • 您的生产环境似乎与您的开发/测试环境有很大不同。我会先尝试解决这个问题。这 500 个错误可能会在该过程中变得明显。
    【解决方案2】:

    你提到 500 Internal Server Error 即将到来。事情在您的本地是正确的,但在服务器上失败了。由于您没有 ssh 访问权限,因此可能很难调试。如果你使用 Docker 或 Kubernetes 之类的东西来构建和部署它会很有用。我可以建议你一些调试方法。代码不会尝试,除非失败,可能的原因是服务器本身由于缺少要求而无法启动,例如某些导入或可能是任何东西。
    调试步骤

  • 创建一个虚拟环境并在其中重新安装要求,类似于您的服务器。这将帮助您确定是否缺少要求。
  • 如果您的环境不是生产环境,并且您正在服务器中测试应用程序,则设置 debug = True。它会在前端显示错误。这绝对不是推荐的方法。我建议因为您没有 ssh 访问权限。
  • 如果可能,请创建一个简单的路由,例如 /hello,然后返回 hello,并检查它是否返回正确的结果。这将让您知道您的服务器是否正在启动。即使这样也不推荐用于生产。
  • 您还可以在请求之前和之后检查烧瓶应用程序。这也可能有用

  • 希望第一步调试对你有很大帮助,你可能不需要进入第 2 步和第 3 步。我给了你最坏的情况

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多