harry
harry

Reputation: 135

Unable to See Python Flask Logs in Azure Web App Service on Linux

I am currently deploying a Python Flask application on Azure Web App Service using a Linux OS. Although I can view the deployment logs without any issues and see print statements in the Kudu logs, I'm unable to view the Flask-specific logs that I would typically see when running the application locally.

Here's what I've already verified and tried:

Application logging is enabled in the Azure portal.

I can see print statements in the Kudu logs.

The application logs properly when run locally.

Flask app.logger statements that should work do not show up in any logs.

Has anyone faced a similar issue, or does anyone have any advice on how to ensure Flask logs are captured correctly in this environment?

Upvotes: 1

Views: 896

Answers (3)

Rajesh
Rajesh

Reputation: 1

I would suggest anyone who is facing this log/prints not coming in azure appservice console can use this:

print("Some information...", data, flush=True)

That's it and it will solve all your issues. šŸš€

Upvotes: 0

As far as my knowledge is concerned Azure Web App for Python uses Gunicorn server in production. Therefore in startup file we need to pass logging parameters while launching gunicorn server:

gunicorn --bind=0.0.0.0 --timeout 600 app:app --workers=4 --access-logfile '-' --error-logfile '-'

Here we are using --access-logfile and --error-logfile parameters.

'-' parameter value indicates that logs will be printed on stdout stream in Log Stream.

For reference: https://learn.microsoft.com/en-us/azure/app-service/configure-language-python

Upvotes: 0

Aslesha Kantamsetti
Aslesha Kantamsetti

Reputation: 1556

I have created flask application and deployed to azure app service and was successfully able to view the logs in kudu files >> log stream.

I have also tried to deploy the application to an Azure Web App using free-tier subscription and was Successfully able to view the logs in log stream.

The Below Code contains the logging configuration that sets whether it's running directly or through a WSGI server like Gunicorn or Uvicorn.

import logging
from flask import Flask
app = Flask(__name__)
if __name__ != '__main__':
    gunicorn_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
else:
    logging.basicConfig(level=logging.INFO)
    app.logger.setLevel(logging.INFO)
@app.route('/')
def hello():
    app.logger.info('Hello world log entry')
    return 'Hello World!'
if __name__ == "__main__":
    app.run()

In Azure web App under Monitoring section -> App Service logs, enable Application logging to File System.

enter image description here

Log Stream logs:

enter image description here

enter image description here

Output:

enter image description here

Upvotes: 1

Related Questions