簡體   English   中英

如何將 GCE 的錯誤報告與 Docker 和 Django 一起使用?

[英]How to use Error Report from GCE with Docker and Django?

我在使用 Docker 的計算引擎上運行 Django。 我想知道當應用程序遇到像雲運行這樣的錯誤時,如何檢查錯誤報告中的錯誤。

我正在研究如何在 Python 中設置錯誤報告。 https://github.com/googleapis/python-error-reporting/tree/main/samples/snippets/fluent_on_compute

查看此示例,我似乎需要引發異常並運行report (traceback.format_exc ())以使用錯誤報告。

示例代碼

def simulate_error():
    fluent.sender.setup('myapp', host='localhost', port=24224)

    def report(ex):
        data = {}
        data['message'] = '{0}'.format(ex)
        data['serviceContext'] = {'service': 'myapp'}
        # ... add more metadata
        fluent.event.Event('errors', data)

    # report exception data using:
    try:
        # simulate calling a method that's not defined
        raise NameError
    except Exception:
        report(traceback.format_exc())

當我運行 Django 時,我得到一個錯誤,而不是使用try: execpt

如何在錯誤報告中顯示此類錯誤?

請讓我知道是否有任何好的方法。 謝謝你。

Django 提供了定義自定義異常處理程序的選項。
您可以定義自定義異常處理程序,如下所示,

from google.cloud import error_reporting
from rest_framework.views import exception_handler

# Initialize Error reporting client
client = error_reporting.Client()

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Send error to Error Reporting
    client.report(response)

    return response

在您的設置文件中添加以下配置,

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

要安裝谷歌雲錯誤報告庫,請使用以下命令

pip install google-cloud-error-reporting

有關在 python 中設置錯誤報告的參考,請參閱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM