Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logger logging same entry numerous times

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

like image 455
Hellnar Avatar asked Aug 12 '10 12:08

Hellnar


People also ask

Why does logger print twice in Python?

Multiple calls to getLogger() with the same name will always return a reference to the same Logger object". NOTE: even if you init a loggers from other modules with same name, you will still get the same logger, therefore calling i.e logger.info('somthing') will log as many times as you initiated the logger class.

Are Python loggers thread-safe?

The logging module can be used directly from multiple threads. The reason is because the logging module is thread-safe.

Which is better between print and logging?

Levels of Severity- The logger module has several levels of severity. The default logging level is warning. Print- The only time when print() is a better option than logging is when the goal is to display a help statement for a command line application.


2 Answers

You are adding a new FileHandler to the root logger every time you call that function: the call to logger.getLogger() without a name argument returns the same logger object every time.

You should call generate_logger() only once and then simply get the same logger object by calling logger.getLogger():

generate_logger()

# .... some time later

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

(note that you do not need generate_logger() to return a value now)

like image 86
Stijn Hoop Avatar answered Sep 18 '22 14:09

Stijn Hoop


I also faced the same problem and came across this page. Yes, I was also creating multiple handlers. In generate_logger(), you can check if there's any other handlers and delete them.

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)
    # Reset the logger.handlers if it already exists.
    if logger.handlers:
        logger.handlers = []
    fh = logging.FileHandler(LOG_FILENAME)
    formatter = logging.Formatter(FORMAT)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger
like image 39
mohi666 Avatar answered Sep 20 '22 14:09

mohi666