From 2810772d5e2c54fe654cbff6c644f27ce5390a0e Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Thu, 7 Sep 2023 17:13:08 -0700 Subject: [PATCH 1/2] Update README.md with logging info --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 31ea1cc66a..b7a3d86804 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,29 @@ The ask tab uses the approach programmed in [retrievethenread.py](https://github There are also two other /ask approaches with a slightly different approach, but they aren't currently working due to [langchain compatibility issues](https://github.com/Azure-Samples/azure-search-openai-demo/issues/541). +
+How can we view logs from the App Service app? + +You can view production logs in the Portal using either the Log stream or by downloading the default_docker.log file from Advanced tools. + +To log additional things, first set the level of logs you want to see by writing this code inside the `create_app()` function in `app.py`: + +```python +logging.basicConfig(level=logging.INFO) +``` + +Change the `INFO` to `DEBUG` or `WARN` as needed. + +Then, inside a route handler, make calls using the global variable `current_app`'s logger method: + +```python +current_app.logger.info("Received /chat request") +``` + +Test that change locally, and if it looks good, you can re-deploy the code with `azd deploy`. + +If you're having troubles finding the logs in App Service, see this blog post on [tips for debugging App Service app deployments](http://blog.pamelafox.org/2023/06/tips-for-debugging-flask-deployments-to.html) or watch [this video about viewing App Service logs](https://www.youtube.com/watch?v=f0-aYuvws54). +
### Troubleshooting @@ -314,5 +337,4 @@ Here are the most common failure scenarios and solutions: 1. You see `CERTIFICATE_VERIFY_FAILED` when the `prepdocs.py` script runs. That's typically due to incorrect SSL certificates setup on your machine. Try the suggestions in this [StackOverflow answer](https://stackoverflow.com/questions/35569042/ssl-certificate-verify-failed-with-python3/43855394#43855394). -1. After running `azd up` and visiting the website, you see a '404 Not Found' in the browser. Wait 10 minutes and try again, as it might be still starting up. Then try running `azd deploy` and wait again. If you still encounter errors with the deployed app, consult these [tips for debugging App Service app deployments](http://blog.pamelafox.org/2023/06/tips-for-debugging-flask-deployments-to.html) -and file an issue if the error logs don't help you resolve the issue. +1. After running `azd up` and visiting the website, you see a '404 Not Found' in the browser. Wait 10 minutes and try again, as it might be still starting up. Then try running `azd deploy` and wait again. If you still encounter errors with the deployed app, consult these [tips for debugging App Service app deployments](http://blog.pamelafox.org/2023/06/tips-for-debugging-flask-deployments-to.html) or watch [this video about downloading App Service logs](https://www.youtube.com/watch?v=f0-aYuvws54). Please file an issue if the logs don't help you resolve the error. From 2735b83921d08fb8b3a9f9e3788005bb71aa14ea Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 8 Sep 2023 04:31:10 +0000 Subject: [PATCH 2/2] Build logging in with env var --- README.md | 26 +++++++++++++++++--------- app/backend/app.py | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b7a3d86804..2a31b733d2 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ The repo includes sample data so it's ready to try end to end. In this sample ap * **Azure subscription with access enabled for the Azure OpenAI service**. You can request access with [this form](https://aka.ms/oaiapply). * **Azure account permissions**: Your Azure account must have `Microsoft.Authorization/roleAssignments/write` permissions, such as [Role Based Access Control Administrator](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#role-based-access-control-administrator-preview), [User Access Administrator](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#user-access-administrator), or [Owner](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#owner). -## Azure deployment +## Azure deployment ### Cost estimation @@ -73,7 +73,7 @@ either by deleting the resource group in the Portal or running `azd down`. ### Project setup -You have a few options for setting up this project. +You have a few options for setting up this project. The easiest way to get started is GitHub Codespaces, since it will setup all the tools for you, but you can also [set it up locally](#local-environment) if desired. @@ -302,25 +302,33 @@ There are also two other /ask approaches with a slightly different approach, but
How can we view logs from the App Service app? -You can view production logs in the Portal using either the Log stream or by downloading the default_docker.log file from Advanced tools. +You can view production logs in the Portal using either the Log stream or by downloading the default_docker.log file from Advanced tools. -To log additional things, first set the level of logs you want to see by writing this code inside the `create_app()` function in `app.py`: +The following line of code in `app/backend/app.py` configures the logging level: ```python -logging.basicConfig(level=logging.INFO) +logging.basicConfig(level=os.getenv("APP_LOG_LEVEL", "ERROR")) ``` -Change the `INFO` to `DEBUG` or `WARN` as needed. +To change the default level, you can set the `APP_LOG_LEVEL` environment variable locally or in App Service +to one of the [allowed log levels](https://docs.python.org/3/library/logging.html#logging-levels): +`DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`. -Then, inside a route handler, make calls using the global variable `current_app`'s logger method: +If you need to log in a route handler, use the the global variable `current_app`'s logger: ```python -current_app.logger.info("Received /chat request") +async def chat_stream(): + current_app.logger.info("Received /chat request") ``` -Test that change locally, and if it looks good, you can re-deploy the code with `azd deploy`. +Otherwise, use the `logging` module's root logger: + +```python +logging.info("System message: %s", system_message) +``` If you're having troubles finding the logs in App Service, see this blog post on [tips for debugging App Service app deployments](http://blog.pamelafox.org/2023/06/tips-for-debugging-flask-deployments-to.html) or watch [this video about viewing App Service logs](https://www.youtube.com/watch?v=f0-aYuvws54). +
### Troubleshooting diff --git a/app/backend/app.py b/app/backend/app.py index 045c2a8c11..c130d73390 100644 --- a/app/backend/app.py +++ b/app/backend/app.py @@ -228,5 +228,6 @@ def create_app(): app = Quart(__name__) app.register_blueprint(bp) app.asgi_app = OpenTelemetryMiddleware(app.asgi_app) - + # Level should be one of https://docs.python.org/3/library/logging.html#logging-levels + logging.basicConfig(level=os.getenv("APP_LOG_LEVEL", "ERROR")) return app