I have created a Python Azure Functions app. In this application I want to check log details like DEBUG
, INFO
etc. I have written some code for logging purpose, but I am not able to get any log after executing my azure function application.
I have written basic code for logging purpose as below but after executing Azure Functions I am not able to see the log on console.
import logging
import azure.functions as func
data = "Hello"
logging.basicConfig(level=logging.DEBUG)
logging.debug(data)
Is there any other solution or workaround for the above problem?
Python has a built-in module logging which allows writing status messages to a file or any other output streams. The file can contain the information on which part of the code is executed and what problems have been arisen.
From the Monitoring section of your function app in the Azure portal, select Diagnostic settings, and then select Add diagnostic setting. In the Diagnostics settings page, under Category details and log, choose FunctionAppLogs.
Debugging is an important step of any software development project. The logging module is part of the standard Python library, provides tracking for events that occur while software runs, and can output these events to a separate log file to allow you to keep track of what occurs while your code runs.
Most likely the root logger got messed with by azure and basicConfig
only creates a root logger with some sane defaults. To work around this create your own independent logger.
import logging
logger = logging.getLogger('akshay')
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
logger.addHandler(sh)
logger.debug('Hello')
# Outputs 'Hello'
This GitHub issue explains the solution:
As for the original issue noted here, the desired behavior can be achieved with a combination of two steps:
- Setting the root logger in user code to use the debug level (it is set to to info by default).
- Configuring the host.json to log Debug messages: [...]
Specifically, this is the code that you need in your script:
import logging
logging.Logger.root.level = 10
logging.debug("Debug message here")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With