Python logger continuiing to fail to log

Sometimes I think Python has the weirdest logic to it.

Why does the Python logger function fail to log a single thing when it’s in a def function but logs everything when it’s outside a def function?

I have created a MRE as a tester, but I see no reason as to why it chooses to log everything outside a def function but nothing inside a def function.

My logging configuration file is correctly imported, and everything that is logged outside of the test_logging() function is logged & logged to the correct .log file.

As you cansee the setup in the logging_config file is set to DEBUG so it will catch everything.

This is my setup, starting with the logging configuration file:

import logging

#<ROOT>.logging_config
def logging_configuration(filename="support/app.log"):
    logging.basicConfig(level=logging.DEBUG,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[logging.FileHandler(filename),
            logging.StreamHandler()])

This is the MRE test:

#support.views
from <ROOT>.logging_config import logging_configuration
import logging

logging_configuration()
logger = logging.getLogger(__name__)
logger.debug("Logging setup complete...")

logger.debug("Debug message not in a def function")
logger.info("Info message not in a def function")
logger.warning("Warning message not in a def function")
logger.error("Error message not in a def function")
logger.critical("Critical message not in a def function")

def test_logging():
    logger.debug("Debug message")
    logger.info("Info message")
    logger.warning("Warning message")
    logger.error("Error message")
    logger.critical("Critical message")
    print("You should see this message")

if __name__ == "__main__":
    test_logging()

These are the logs:

2024-08-07 09:53:55,090 - support.views - DEBUG - Logging setup complete...
2024-08-07 09:53:55,091 - support.views - DEBUG - Debug message not in a def function
2024-08-07 09:53:55,091 - support.views - INFO - Info message not in a def function
2024-08-07 09:53:55,091 - support.views - WARNING - Warning message not in a def function
2024-08-07 09:53:55,092 - support.views - ERROR - Error message not in a def function
2024-08-07 09:53:55,092 - support.views - CRITICAL - Critical message not in a def function

To me, it makes absolutely no sense. What am I missing here?

Seems like that function isn’t being called. How are you executing the file?

This is how I’m calling the function

That is not the question I asked, I asked how you are executing the script. Because wether the function gets run depends on how the file is executed.

You asked how I am executing the file ad I replied with the following:

There is a difference between function and file. I don’t have the patience to explain such basic terms to you, maybe someone else will.

If you are importing that file, its __name__ is not '__main__', so the function call is not going to be executed.

Thank you, now we’re getting somewhere. So does this mean that I will need to change it to :

if name == “views”:
test_logging()

Many thanks

Sorry @MegaIng I misread that, the way I am executing the file, in this test, is by running the server, however, its a views.py file which is executed by calling the relevant functions from submitting a web form, which process the data and sends it do my CRM system.

Then yeah, the views.py is not being executed as a main module, and therefore the __main__ guard isn’t being executed, which means your test_logging function isn’t being run.

Instead it is getting imported and django is going to take the attributes corresponding to views you tell it about and run those functions on demand.

So you could specify a test_logging path that calls this function as a view. That’s probably the best end-to-end test for this logging stuff.

Having the __name__ == "views" guard you suggest is pointless. That would only execute code if this isn’t the main module, which is rarely (but not never) useful. If you want to execute the function on import, drop the if guard completely and just call the function.

1 Like

Yeah, sounds good. Thanks for your help.