Craig
Craig

Reputation: 1985

Logging from multiple modules to the same log file

I have a simple project structure - my main module calls two other modules residing within the same directory. I followed the instructions per this answer to set up my test.

My code is as follows:

Main module:

# log_test.py
import logging
import imported_1
import imported_2

def main():

    logger = logging.getLogger(__name__)
    logging.basicConfig(filename="test.log", format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logger.setLevel(logging.DEBUG)
    logger.debug("This is a debug message")
    logger.info("For your info")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("This is a critical message")

    imported_1.one_fn(5, 6)
    imported_2.two_fn(10, 20)

if __name__ == '__main__':
    main()

The imported modules - imported_1.py

# imported_1.py
import logging
logger = logging.getLogger(__name__)

def one_fn(x, y):
    print(f"Logging function one with {x} and {y}")
    logger.info(f"Logging function one with {x} and {y}")

And imported_2.py

# imported_2.py
import logging
logger = logging.getLogger(__name__)

def two_fn(x, y):
    print(f"Logging function two with {x} and {y}")
    logger.info(f"Logging function one with {x} and {y}")

The generated log file test.log only contains entries from the main log_test.py module. The imported modules are not logged here:

2019-12-21 18:26:41,351 - __main__ - DEBUG - This is a debug message
2019-12-21 18:26:41,351 - __main__ - INFO - For your info
2019-12-21 18:26:41,351 - __main__ - WARNING - This is a warning message
2019-12-21 18:26:41,351 - __main__ - ERROR - This is an error message

I am looking for log messages from the imported modules to show up in the same log file as specified by basicConfig. What am I doing wrong here?

Upvotes: 2

Views: 525

Answers (1)

Ron Serruya
Ron Serruya

Reputation: 4426

In order for the loggers of imported1 and import2 to know the original logger you created, they need to be its children.
In python loggers are arranged by '.', so logger a.b.c is a child of logger a.b which is a child of a

You can achieve what you want this way:
In log_test.py

logger = logging.getLogger('my_logger')

In imported_1.py

logger = logging.getLogger('my_logger.' + __name__) # will be my_logger.imported_1

In imported_2.py

logger = logging.getLogger('my_logger.' + __name__) # will be my_logger.imported_2

You can check out more here https://docs.python.org/3/howto/logging-cookbook.html#using-logging-in-multiple-modules

Upvotes: 2

Related Questions