Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging DEBUG logs are not shown when executing the Python Azure Functions

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?

like image 755
Akshay Godase Avatar asked Jul 23 '19 11:07

Akshay Godase


People also ask

Which function is are performed by logging in Python?

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.

How do I check Azure function logs?

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.

What is logging debug in Python?

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.


2 Answers

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'
like image 173
blues Avatar answered Oct 23 '22 11:10

blues


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:

  1. Setting the root logger in user code to use the debug level (it is set to to info by default).
  2. 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")
like image 25
weebsnore Avatar answered Oct 23 '22 12:10

weebsnore