views:

41

answers:

2

I have a such logger initializing function:

def generate_logger():
    import logging
    LOG_FILENAME = os.path.join(PROJECT_DIR, "mylog.log")
    FORMAT = "%(asctime)s : %(message)s"
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler(LOG_FILENAME)
    formatter = logging.Formatter(FORMAT)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger

And at some part of my code I have such exception catching:

logger = generate_logger()
except AttributeError:
    logger.error('Opps we got an error')

Weirdly I get same error written 2 times and it can be caugh only once, once I change logger.error('Opps we got an error') with print "test", I get "test" printed once.

What can be the problem and the solution.

Regards

A: 

You probably have two handlers going to the same resulting log.

How many handlers are you creating? How many times are you executing generate_logger? Each time you execute generate_logger you'll create another handler to the same file, leading to potential duplication.

S.Lott
A: 

I think you're probably getting two handlers added to the logger somehow. Perhaps at some point an implicit handler is being added.

Simon Hibbs