最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - I'm having issues with logging, is there anyway to fix this? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Feb 3 at 19:40 picobit 7037 silver badges17 bronze badges asked Feb 3 at 13:13 D3AD_SHOTD3AD_SHOT 336 bronze badges 1
  • 1 What are you expecting and why? Please edit and trim your code to a minimal reproducible example and remove the unrelated parts, e.g. pyautogui and all the image handling seems unrelated. – Friedrich Commented Feb 3 at 15:25
Add a comment  | 

1 Answer 1

Reset to default 2

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.

发布评论

评论列表(0)

  1. 暂无评论