Setting DEBUG log level in python azure function

JDR 90 Reputation points
2023-10-17T06:39:56.21+00:00

Hi,

I am successfully loging messages from my Python Azure function (http trigger) to App Insights at the INFO level but my DEBUG logs are not showing.

The following doesn't solve the problem:

import logging

logging.basicConfig(level=logging.DEBUG)

@app.route(route="test", methods=['GET'])
def test(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
	logging.debug('Some message')
	...


I also tried to modify hosts.json to the following:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "Function": "Debug"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

(per https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#configure-log-levels)

Doesn't work either.

Thanks in advance for any suggestion.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. navba-MSFT 20,560 Reputation points Microsoft Employee
    2023-10-17T11:30:45.5066667+00:00

    @JDR Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I understand that you are having trouble logging DEBUG messages from their Python Azure function to App Insights. You have tried modifying the logging level and host.json file, but the DEBUG logs are still not showing.

    I was able to create a simple Python HTTPTrigger function app and deployed it to Azure. I have the below lines of code added in my app:

        // Add the import statement
        import logging
    
        // Then you can add the below lines in your code. 
        logging.Logger.root.level = logging.DEBUG
        logger = logging.getLogger('MyHttpFunc')
        
        logger.setLevel(logging.DEBUG)
        logging.debug('This is a debug log.')
    
        logging.info('This is an info log')
        logging.info('Python HTTP Triggered function processed a request.')
    
    

    I have the below within my host.json:

    {
      "version": "2.0",
      "logging": {
        "logLevel": {
          "Host.Results": "Error",
          "Function": "Debug",
          "Host.Aggregator": "Trace"
        },
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.*, 4.0.0)"
      }
    }
    

    While the HTTP Trigger was invoked and executed, I could see the debug statements in the App Insights trace logs:

    User's image

    User's image

    Also note that for a function app to send data to Application Insights, it needs to know the instrumentation key of an Application Insights resource. The key must be in an app setting named APPINSIGHTS_INSTRUMENTATIONKEY.

    Hope this helps.

    If you have any follow-up questions, please let me know. I would be happy to help.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful