Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging duplicated

Tags:

python

logging

I have Four files,

  • Main.py
  • A.py
  • B.py
  • Log_System

i am using main to use functions of A.py and B.py, so now i have to log all the information when ever i call them.

so i wrote a script called log_system to create log handler for each script file such as A.py, B.py

import logging

def fetchLogger(name="None") :
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    if (name == "None"):
        #create File for Log
        handler = logging.FileHandler('./engine_log/Generic.log')
        handler.setLevel(logging.DEBUG)
        #log format 
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)

        #adding the handler to Logging System
        logger.addHandler(handler)
    else: 
        #create File for Log
        handler = logging.FileHandler('./engine_log/'+str(name))
        handler.setLevel(logging.DEBUG)
        #log format 
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        #adding the handler to Logging System
        logger.addHandler(handler)
    return logger

so if i want to use logging in script file A.py, i would write these line:

import log_system 
"""Log System Building """
file_name =  'A.py'
logger = log_system.fetchLogger(file_name)

def hello():
    try:
        logger.info("excuting Hello")
    except: 
        logger.debug("something went wrong in hello")

but my log files

2017-10-18 14:59:28,695 - log_system - INFO - A.py-excuting Hello
2017-10-18 14:59:28,695 - log_system - INFO - A.py-excuting Hello
2017-10-18 14:59:28,695 - log_system - INFO - A.py-excuting Hello
2017-10-18 14:59:28,695 - log_system - INFO - A.py-excuting Hello
2017-10-18 14:59:28,695 - log_system - INFO - A.py-excuting Hello

it is repeating the log many times.... so what should i do ??

solution

logger = logging.getLogger(name)

if logger.hasHandlers(): 
    logger.handlers = []

logger.setLevel(logging.DEBUG)

#create File for Log
handler = logging.FileHandler('./engine_log/'+str(name))
handler.setLevel(logging.DEBUG)
#log format 
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
#adding the handler to Logging System
logger.addHandler(handler)

return logger

this is how i changed my log_system code, so i just emptied the Handler list if i had created an log handler already so that it does not create duplicate records.

like image 635
Sriram Arvind Lakshmanakumar Avatar asked Jul 13 '26 12:07

Sriram Arvind Lakshmanakumar


1 Answers

Every time fetch_logger is called it adds a new FileHandler to the logger. Each FileHandler writes to the log file, resulting in the repeating output in the file.

One solution is to call the logger's hasHandlers method. This will return True if any handlers have been configured on the logger, and then you can delete them.

def fetchLogger(name="None") :
    logger = logging.getLogger(__name__)
    if logger.hasHandlers():
        # Logger is already configured, remove all handlers
        logger.handlers = []
    # Configure the logger as before.
    ...
like image 95
snakecharmerb Avatar answered Jul 15 '26 01:07

snakecharmerb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!