How do you configure logging properly in a multi threaded python function app?

Nick Petzold 5 Reputation points
2023-05-04T05:54:26.98+00:00

I'm building a python function app which utilises multithreading for performance reasons and have run into issues getting the logs to be emitted properly from non-main threads.

I've found this Stack Overflow Answer, which solves the issue, however when I add the azure-functions-worker to my requirements.txt, I get the following warning:

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions-worker==1.1.9
...

I'm not detecting any unusual behaviour as a result, but obviously wary about doing something the platform specifically warns against. It's also imperative that I have comprehensive logging from the non-main threads, so I'd be very keen to find a proper solution to this.

Thanks in advance!

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

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-05-08T17:13:13.33+00:00

    The reason you're receiving the warning is because it's automatically managed by the platform. It gets updated with new features and bug fixes. You can read more at Azure Functions Python worker dependencies. Using an older/different version could have unintended consequences.

    If logging via

    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    

    isn't working as expected, you can also look at monitoring through Application Insights.

    import logging
    from azure.functions.logging import AppInsightsHandler
    
    app_insights_handler = AppInsightsHandler()
    logging.getLogger().addHandler(app_insights_handler)
    

    And leverage the logging object for .info(), .warning(), and .error().


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.