-1

I am getting duplicate logs for each iteration in Pytest Parametrize fixture.

Sample File -

input1 = 1
input2 = 2
input3 = 3


@pytest.mark.parametrize("input",[(input1),(input2),(input3)])
def test_numbers(input):
    file_name = inspect.stack()[0][3]
    log = customLogger(file_name=file_name)
    log.info(input)

Logger Code -

def customLogger(file_name,log_level=logging.INFO):
    '''
    Important Logging Information
    CRITICAL = 50
    FATAL = CRITICAL
    ERROR = 40
    WARNING = 30
    WARN = WARNING
    INFO = 20
    DEBUG = 10
    NOTSET = 0
    '''
    # Gets the name of the class / method from where this method is called
    logger_name = inspect.stack()[1][3]
    logger = logging.getLogger(logger_name)
    # By default, log all messages
    logger.setLevel(logging.DEBUG) # dont change this setting

    file_handler = logging.FileHandler("../logs/"+file_name +".log", mode='a')
    file_handler.setLevel(log_level)

    formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s: %(message)s',
                                  datefmt='%m/%d/%Y %I:%M:%S %p')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    return logger

I get a response in my log file like this -

07/07/2020 02:01:52 AM - test_number - INFO: 1
07/07/2020 02:01:52 AM - test_number - INFO: 2
07/07/2020 02:01:52 AM - test_number - INFO: 2
07/07/2020 02:01:52 AM - test_number - INFO: 3
07/07/2020 02:01:52 AM - test_number - INFO: 3
07/07/2020 02:01:52 AM - test_number - INFO: 3

I am out of my mind in resolving this, I am thinking to write a Log cleaner method to remove duplicates from the logs. Please let me know if I am missing something here ?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ezio
  • 376
  • 5
  • 21
  • 1
    Each time you call `customLogger`, you create and add a new logging handler, that will also log the line, thus the multiplication of logs. Make sure to only create one handler, or one custom logger. – MrBean Bremen Jul 06 '20 at 20:56
  • Thanks @MrBeanBremen for the suggestion, could you please suggest where I can make this change? I am going bonkers looking at this issue from hours now.. – Ezio Jul 06 '20 at 21:01

1 Answers1

1

Each time you call customLogger, you create and add a new logging handler, that will also log the line, thus the multiplication of logs.

There are several possibilities to ensure that, the easiest is probably just to clear the handlers before adding a new one:

... 
file_handler.setFormatter(formatter)
logger.handlers = []
logger.addHandler(file_handler)
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46