From 7e00d50f183c5f157d4534599ee81e2d3bb30107 Mon Sep 17 00:00:00 2001 From: Nicola Tarocco Date: Thu, 12 Oct 2023 21:21:30 +0200 Subject: [PATCH] debugging: add tips to debug on deployed env --- docs/develop/getting-started/debugging.md | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/develop/getting-started/debugging.md b/docs/develop/getting-started/debugging.md index 4edcf410..5be56b91 100644 --- a/docs/develop/getting-started/debugging.md +++ b/docs/develop/getting-started/debugging.md @@ -35,6 +35,32 @@ Last, but not least you can print SQL queries to the console by setting the `SQL SQLALCHEMY_ECHO = True ``` +**Logging output** + +If you need to debug some code in a deployed environment (hard to debug locally), you can print the logs to +the standard output or to a file. +To see the logs in the terminal (e.g. in a container log), you can do the following: + +1. prepare your code with the necessary logging instructions: +```python +from flask import current_app + ... + current_app.logger.info(f"my variable: {my_variable}") +``` +2. make sure that the log level of Flask is at least the same as your log instructions, in this case `info`. +This is needed because Flask does not set a log level when running in non-DEBUG mode: +```python +def my_func(): + import logging + current_app.logger.setLevel(logging.INFO) + current_app.logger.root.setLevel(logging.INFO) + current_app.logger.handlers[0].setLevel(logging.INFO) # handlers[0] is normally the stdout handler + ... +``` +3. deploy and run: you should now see all your logs. + +You can customize this, for example, with a file handler. Refer to the Python documentation for more information. + ## JavaScript All modern browsers have a built-in JavaScript debugger. You can enable the debugger in your browser and take advantage of the below 2 main ways to debug your JS application: