How to fix wrong level classification in App Service Logs

Matt Schwartz 20 Reputation points
2024-12-18T16:48:06.11+00:00

I set up logging in our Python App Service and when looking at the console logs, Azure is misclassifying the level of the logs. For example, a simple info log shows as an error when querying the logs. Here is a screenshot:

User's image

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,095 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shree Hima Bindu Maganti 1,585 Reputation points Microsoft Vendor
    2024-12-20T19:48:00.4933333+00:00

    Hi Matt Schwartz

    Welcome to the Microsoft Q&A Platform!
    The issue you're facing seems to be related to the misclassification of log levels in Azure App Service Console Logs.

    Ensure your logging levels are set correctly in the Python app.

    import logging  
    logging.basicConfig(level=logging.INFO)  
    logger = logging.getLogger(__name__)  
    logger.info("This is an INFO log.")  
    logger.error("This is an ERROR log.")  
    

    Go to Azure Portal > App Service > Diagnostic Settings.

    Ensure logs for Application Logs and Console Logs are enabled and categories are correctly configured.
    If using middleware Pass the correct log level when starting the application.
    uvicorn app.main:app --log-level info
    Implement a handler to explicitly map log levels.

     class AzureLogHandler(logging.Handler):  
        def emit(self, record):  
            level = record.levelname  
            if level == "INFO":  
                print(f"INFO:{record.getMessage()}")  
            elif level == "ERROR":  
                print(f"ERROR:{record.getMessage()}")  
    handler = AzureLogHandler()  
    logger.addHandler(handler)  
    

    Use Log Analytics Workspace to query log levels with KQL.

    AppServiceConsoleLogs  
    | where Level == "Error"  
    | project TimeGenerated, ResultDescription, Level  
    

    Use Application Insights or third-party tools (e.g., ELK Stack) to validate the log levels independently.
    ref: Python Logging Documentation
    https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/diagnostic-settings
    https://learn.microsoft.com/en-us/kusto/query/kql-quick-reference?view=azure-data-explorer&preserve-view=true
    https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview
    https://learn.microsoft.com/en-us/azure/azure-portal/supportability/how-to-create-azure-support-request
    If the answer is helpful, please click ACCEPT ANSWER and kindly upvote

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.