S Andrew
S Andrew

Reputation: 7208

Python logging show 3 lines for 1 log in log file

I have a python project where I have 3 python files out of which app.py is the main file and other two files are tracker.py and celerio.py. I have the logging enabled for all the files and below is its code:

def setup_logger(logger_name, log_file, level=logging.ERROR):
    log_formatter = logging.Formatter('%(asctime)s %(message)s')
    my_handler = RotatingFileHandler(log_file, mode='a', maxBytes=100 * 1024 * 1024, backupCount=5, encoding=None,
                                     delay=0)
    my_handler.setFormatter(log_formatter)
    my_handler.setLevel(level)
    l = logging.getLogger(logger_name)
    l.addHandler(my_handler)

dir = Path(__file__).parents[1]
log_file = os.path.join(dir, 'logs', 'application.log')
setup_logger('debug', log_file)
log = logging.getLogger('debug') 

and then I use it like log.error("Hello world") in the python file and it works fine. Now as I have another 2 files, I have also copied the same above code in those two files tracker.py and celerio.py. But the problem is, lets if I log log.error("Hello world from app.py") in app.py. It is saved as 3 times in application.log file. Same is the case with other files as well.

2019-11-15 13:32:00,288 Hello world from app.py
2019-11-15 13:32:00,288 Hello world from app.py
2019-11-15 13:32:00,288 Hello world from app.py

I dont know why it is logged 3 times for just 1 log. Can anyone please help me in this. Thanks

Upvotes: 0

Views: 103

Answers (2)

Daniel Hepper
Daniel Hepper

Reputation: 29967

The logging system has global state. By adding this code to all three of your modules, you are adding three handlers to the logger named "debug".

Only setup your logging once in app.py, only use log = logging.getLogger('debug') in your other modules.

app.py

def setup_logger(logger_name, log_file, level=logging.ERROR):
    log_formatter = logging.Formatter('%(asctime)s %(message)s')
    my_handler = RotatingFileHandler(log_file, mode='a', maxBytes=100 * 1024 * 1024, backupCount=5, encoding=None,
                                     delay=0)
    my_handler.setFormatter(log_formatter)
    my_handler.setLevel(level)
    l = logging.getLogger(logger_name)
    l.addHandler(my_handler)

dir = Path(__file__).parents[1]
log_file = os.path.join(dir, 'logs', 'application.log')
setup_logger('debug', log_file)
log = logging.getLogger('debug') 

tracker.py and celerio.py

import logging
log = logging.getLogger('debug') 

Upvotes: 1

ababak
ababak

Reputation: 1793

While you set up your logger you add new handlers over and over again. Just try to clear them before adding:

l.handlers[:] = []

Upvotes: 1

Related Questions