Nikita
Nikita

Reputation: 73

How do we get the exceptions handled in python flask code in azure web app logs

I have a flask app run on azure web app. For debug purpose, we have print statements in exception handling and other places to check the progress of the code. However i'm unable to find these print statements in azure logs.

While working on VM it was easier to find the logs on the console.

Note: i have tried with app service logs and diagnostic logs. However i'm not able to find the custom print statement in azure logs.

How do we get logging in azure to have the custom messages in the python flask code?

try:
    self.asset_name = req_json['assetname']
except KeyError:
    print("Asset Name cannot be empty")
    return "Asset Name cannot be empty"

Upvotes: 1

Views: 1451

Answers (1)

Nikita
Nikita

Reputation: 73

So after a few trying around the azure diagnostic logs, i found that using the logging module of python can be useful. The print statements do occur but not as expected. So the solution which worked for me:

import logging


logger = logging.getLogger(__name__)
logging.basicConfig(format='Custom Logs: %(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

try:
    self.asset_name = req_json['assetname']
except KeyError:
    logger.exception("Asset Name cannot be empty")
    return "Asset Name cannot be empty"

These logs can be found in the console logs of the diagnostic logs which we need to turn on in azure.

Upvotes: 1

Related Questions