Search code examples
pythonloggingpyautoguipython-logging

I'm having issues with logging, is there anyway to fix this?


is there away to make this code not spam in the second file without interfering with any other functions? "Anomaly.log" is being spammed although "Bandit.log" is not being spammed, what am I doing wrong?

import pyautogui
import schedule
import time
import logging


# Initialize a counter
call_count = 0


# Example calls


def logger():
    logging.basicConfig(level=logging.INFO, filename="bandit.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s")
    logger = logging.getLogger(__name__)
    logger.propagate = False

    handler = logging.FileHandler('anomaly.log')
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    try:
        location = pyautogui.locateOnScreen('1.png', confidence=0.9)
        if pyautogui.locateOnScreen('1.png', confidence=0.9):
            global call_count
            call_count += 1
            logging.info(call_count)
    except pyautogui.ImageNotFoundException:
        pass

def bandit_cycle():
    try:
        location = pyautogui.locateOnScreen('1.png', confidence=0.9)
        logging.info('x1 Data found')
        time.sleep(1.5)
    except:
        pass

# schedule for main working code
schedule.every(1).seconds.do(bandit_main)

# schedule for main working code
schedule.every(1).seconds.do(logger)

while True:
    schedule.run_pending()
    time.sleep(1)

Anomaly.log

2025-02-03 14:51:19,773 - main - INFO - 1
2025-02-03 14:51:19,773 - main - INFO - 1
2025-02-03 14:51:19,773 - main - INFO - 1
2025-02-03 14:51:19,773 - main - INFO - 1
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2
2025-02-03 14:51:57,076 - main - INFO - 2

Bandit.log

2025-02-03 14:51:15,548 - INFO - x1 Data found
2025-02-03 14:51:19,773 - INFO - 1
2025-02-03 14:51:52,877 - INFO - x1 Data found
2025-02-03 14:51:57,076 - INFO - 2
2025-02-03 14:52:30,495 - INFO - x5 Data found


Solution

  • When you call this function the first time:

    logger = logging.getLogger(__name__)
    

    It creates a new logger object.

    But if you call it again within the same program execution, it remembers the logger object you already created and just fetches it, instead of creating a new one.

    And then each time you call this:

    logger.addHandler(handler)
    

    It adds another handler to the logger object.

    So you end up with a logger that has dozens, or even hundreds, of handlers. And messages are logged by each handler on the logger.

    You should rearrange your logging setup/initialization code so it isn't called multiple times.