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?