9

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?

2
  • 1
    Probably azure is messing with the level of the root logger. Use logger = logging.getLogger(name) to get a different logger and use that one. Commented Jul 23, 2019 at 11:30
  • @blues I tried logger = logging.getLogger(name) but not getting any result can you please help me? Commented Jul 23, 2019 at 11:52

2 Answers 2

8

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'
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason they do not recommend doing this in the Azure documentation itself, which is too bad since this approached was needed for our team to get logging to work. learn.microsoft.com/en-us/azure/azure-functions/…
@BenGoodman You can access the root logger azure uses too if you just need to set the level: rootlogger = logging.getLogger() (no argument on getLogger() gives the root) and rootlogger.setLevel(logging.DEBUG)
I think if you do this you lose monitoring of the performance and errors in app insights.
6

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")

1 Comment

This is a valid solution! Havinglogging.Logger.root.level = 10 in my function code enables full logging control from host.json-file. Without level 10, only information-level logging works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.