Need help in understanding the custom logger behavior in python

Viewed 15

I have the following modules in python and I am creating custom logger in each module by calling the function in logging_module.app_log.AppLog Class, which returns a new logger for each module.

|--logging_module
|    |
|    |_app_log.py
|--module1
|    |
|    |_program.py
|--module2
|    |
|    |_program.py
|--main_module
    |
    |_main_prg.py

The code in the logging_module.app_log.py is

import logging
import sys


class AppLog:
    def __init__(self):
        pass

    def get_logger(self, logger_name):
        # Create custom logger
        logger = logging.getLogger(logger_name)

        # Add level to Logger
        logger.setLevel(logging.INFO)

        # Create Handlers
        console_handler = logging.StreamHandler(sys.stdout)
        file_handler = logging.FileHandler(r"C:\Users\xyz\Downloads\log\custom_log.log")

        # Set Levels for Handlers
        console_handler.setLevel(logging.INFO)
        file_handler.setLevel(logging.INFO)

        # Set Formatters and add it to Handlers
        consoler_handler_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        console_handler.setFormatter(consoler_handler_format)
        file_handler.setFormatter(file_handler_format)

        # Add Handlers to Logger
        logger.addHandler(console_handler)
        logger.addHandler(file_handler)

        return logger

The code in Module_1.Module1prg is

from logging_module.app_log import AppLog
from module_2.program import Module2prg

class Module1prg:
    def __init__(self):
        self.loggr = AppLog().get_logger(__name__)

    def set_az_clients(self):
        self.loggr.info("setting the blob client object")
        blob_obj = Module2prg()
        blob_obj.get_client()

        self.loggr.info("setting the adls client object")
        adls_obj = Module2prg()
        adls_obj.get_client()

The code in Module_2.Module2prg is:

from logging_module.app_log import AppLog

class Module2prg:
    def __init__(self):
        self.loggr = AppLog().get_logger(__name__)

    def get_client(self):
        self.loggr.info("Returning the client object")

The code in main program is:

from logging_module.app_log import AppLog
from module_1.program import Module1prg

class Mainprg:
    def __init__(self):
        self.m1obj = Module1prg()

    def main(self):
        self.m1obj.set_az_clients()


if __name__ == "__main__":
    loggr = AppLog().get_logger(__name__)
    Mainprg().main()

Since I am calling the same function twice, I am expecting only 2 lines in the output for "Returning the client Object message, But I am getting 3

2022-09-18 21:51:50,146 - module_1.program - INFO - setting the blob client object
2022-09-18 21:51:50,146 - module_2.program - INFO - Returning the client object
2022-09-18 21:51:50,146 - module_1.program - INFO - setting the adls client object
2022-09-18 21:51:50,146 - module_2.program - INFO - Returning the client object
2022-09-18 21:51:50,146 - module_2.program - INFO - Returning the client object

I am creating two objects in set_az_clients function of Module_1.Module1prg class. Will that have any effect? I want to understand why 3 rows are being displayed in the output instead of 2.

I can call the same function twice with the same object. In that case it displays only 2 lines. It seems like creating another object is creating the issue. Can someone help me understand the problem here why the additional line is printing?

0 Answers
Related