View output of Azure function implemented with python on linux consumption plan

Paul Hernandez 686 Reputation points Microsoft Employee
2024-11-06T17:34:52.4333333+00:00

Hi everyone,

I'm creating a couple of azure functions using python and would like to see the logging data when the function is executed. In my local environment, I can see this in vscode terminal:

User's image

My question is, how can I achieve the same in the Azure portal. I cannot find any "log streaming" option under monitoring and I should use application insights according to the documentation, but I don't know how to make it work.

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

1 answer

Sort by: Most helpful
  1. Luis Arias 8,621 Reputation points Volunteer Moderator
    2024-11-06T20:06:11.08+00:00

    Hello Paul, To view logging data for your Python-based Azure Functions in the Azure Portal, similar to what you see in VSCode, use Application Insights. Here’s how to set it up:

    • Enable Application Insights for Your Function App
      Go to your Function App in the Azure Portal > Under Settings, select Application Insights > Click on Enable if it’s not already enabled, and link your Function App to an Application Insights instance. If you don’t have an instance, Azure will prompt you to create one.
    • Configure Logging in Your Python Code
      Make sure to use the logging module in your function code, as shown in your screenshot. Application Insights automatically captures logs created by logging.info, logging.error, etc.
        import logging
        
        @app.function_name(name="BlobTriggerFunction")
        @app.blob_trigger(arg_name="myblob", path="raw/files/{name}.csv",
                        connection="DEPLOYMENT_STORAGE_CONNECTION_STRING__accountname")
        def BlobTrigger(myblob: func.InputStream, context: func.Context):
          logging.info("Python blob trigger function processed blob")
          logging.info(f"Name: {myblob.name}")
          logging.info(f"Blob Size: {myblob.length} bytes")
      
    • View Logs in Application Insights
      Go to Application Insights linked to your Function App > Navigate to Logs under Monitoring > Use the following Log Analytics Query to see your logs:
        traces
        | where customDimensions["Category"] == "Function.<YourFunctionName>"
        | order by timestamp desc
      
      Replace <YourFunctionName> with the actual function name (e.g., BlobTriggerFunction).
    • Use Live Metrics for Real-Time Monitoring
      In Application Insights, go to Live Metrics to see real-time logs and metrics.
    • Alternative: Log Streaming with App Service Logs (Premium Plans)
      If your Function App is on a Premium Plan, you can enable App Service Logs for log streaming.
      Go to Monitoring > App Service Logs > Enable Application Logging (Filesystem) and set the log level > Use Log Stream to view logs in near real-time. User's image
    • For more details:
    • Azure Functions Monitoring with Application Insights

    If the information helped address your question, please Accept the answer.

    Luis

    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.