This is a scenario where a module contains a Logger and it is being imported into another module. When main.py is called however, no LogRecords are written to the log. What can be revised in order for the log to be called?
#objects.py
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter()
log_handler = logging.FileHandler('log_objects.log')
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)
class Person:
def __init__(self):
pass
logger.info(f"A new {self.__class__.__name__} is created.")
#main.py
from my_objects import objects
if __name__ == '__main__':
objects.Person()
-dir/
-my_objects/
__init__.py
log_objects.log
objects.py
-main.py
I'm expecting to see this in the log:
"A new Person is created."
Upon execution nothing shows in the log