アプリケーション・エラーのデバッグ Debugging Application Errors

本番環境上 In Production

本番環境では、開発サーバやデバッガを走らせないように、または組み込みのデバッガを有効にしないようにしてください。デバッガはブラウザから任意のPythonコードを実行できるようにします。それはpinによって保護されていますが、それはセキュリティに対して信頼できるものではありません。 **Do not run the development server, or enable the built-in debugger, in a production environment.** The debugger allows executing arbitrary Python code from the browser. It's protected by a pin, but that should not be relied on for security.

例えばエラーログ処理ツールで説明されているSentryのような、エラー・ログ用のツールを使用するか、もしくはログ処理(Logging)で説明されているようなログ処理および通知を有効にしてください。 Use an error logging tool, such as Sentry, as described in :ref:`error-logging-tools`, or enable logging and notifications as described in :doc:`/logging`.

もしもサーバへアクセスできる場合は、request.remote_addrが自分のIPに合致したときに外部デバッガを開始するためのいくらかのコードを追加できるかもしれません。ある種のIDEのデバッガにもリモート・モードがあり、サーバ上のブレークポイントでローカルとやり取りできます。デバッガを有効にするのは一時的だけにしてください。 If you have access to the server, you could add some code to start an external debugger if ``request.remote_addr`` matches your IP. Some IDE debuggers also have a remote mode so breakpoints on the server can be interacted with locally. Only enable a debugger temporarily.

組み込みのデバッガ The Built-In Debugger

Flaskに組み込まれているWerkzeugの開発サーバは、リクエストの処理中に処理されないエラーが起きたとき、ブラウザの中でやり取りできるtracebackを表示するデバッガを提供します。このデバッガは開発中だけ使用されるべきです。 The built-in Werkzeug development server provides a debugger which shows an interactive traceback in the browser when an unhandled error occurs during a request. This debugger should only be used during development.

実行中のデバッガのスクリーンショット

警告

このデバッガはブラウザから任意のPythonコードを実行できるようにします。それはpinによって保護されていますが、それでも重要なセキュリティリスクを意味します。本番環境では開発サーバやデバッガを走らせないようにしてください。 The debugger allows executing arbitrary Python code from the browser. It is protected by a pin, but still represents a major security risk. Do not run the development server or debugger in a production environment.

開発サーバをデバッグモードで走らせたとき、デバッガは標準で有効になります。 The debugger is enabled by default when the development server is run in debug mode.

$ flask --app hello --debug run

Pythonのコードから走らせたときは、debug=Trueを渡すとデバッグモードが有効になり、(flaskコマンドで走らせたときと)ほとんど同じになります。 When running from Python code, passing ``debug=True`` enables debug mode, which is mostly equivalent.

app.run(debug=True)

デバッガの実行とデバッグ・モードに関するさらなる情報は、開発サーバコマンドライン・インタフェースにあります。デバッガに関するさらなる情報はwerkzeugのドキュメントで見つかります。 :doc:`/server` and :doc:`/cli` have more information about running the debugger and debug mode. More information about the debugger can be found in the `Werkzeug documentation <https://werkzeug.palletsprojects.com/debug/>`__.

外部デバッガ External Debuggers

IDEで提供されるような外部デバッガは、組み込みのデバッガよりも強力なデバッグ体験を提供できます。それらのデバッガはリクエスト処理中にエラーが起きる前にも、もしくはエラーが起きない場合にも、コード上のステップ実行ができます。ある種のデバッガはリモート・モードさえ持っていて、他のマシンで走っているコードをデバッグできます。 External debuggers, such as those provided by IDEs, can offer a more powerful debugging experience than the built-in debugger. They can also be used to step through code during a request before an error is raised, or if no error is raised. Some even have a remote mode so you can debug code running on another machine.

外部デバッガを使用するときは、Flaskアプリはデバッグ・モードのままにしておくべきですが、外部デバッガを邪魔する可能性がある組み込みのデバッガと再読み込み機能(reloader)は無効にすると便利な場合があります。 When using an external debugger, the app should still be in debug mode, but it can be useful to disable the built-in debugger and reloader, which can interfere.

$ flask --app hello --debug run --no-debugger --no-reload

Pythonから走らせるとき: When running from Python:

app.run(debug=True, use_debugger=False, use_reloader=False)

これらの無効化は必須ではなく、外部デバッガは以下の注意を伴いながら働き続けるでしょう。もしも組み込みのデバッガが無効化されていない場合、それは外部デバッグが捕捉する前に処理されてない例外を捕捉するでしょう。もしもreloaderが無効化されていない場合、デバッグ中にコードが変化した場合にそれは予期しない再読み込みを引き起こすかもしれません。 Disabling these isn't required, an external debugger will continue to work with the following caveats. If the built-in debugger is not disabled, it will catch unhandled exceptions before the external debugger can. If the reloader is not disabled, it could cause an unexpected reload if code changes during debugging.