Python Logging Duplicate Write log issues

Source: Internet
Author: User

Python Logging Duplicate Write log issues

When logging with Python's logging module, I encountered the problem of duplicate logging, the first record was written once, the second record was written two times, and the third record was written three times. It's a headache, but it's not a diary. Online search for Causes and solutions:

Reason: No removal of handler
Workaround: After logging is complete removehandler

Before modifying the sample code:

import loggingdef log(message):    logger = logging.getLogger(‘testlog‘)    streamhandler = logging.StreamHandler()    streamhandler.setLevel(logging.ERROR)    formatter = logging.Formatter(‘%(asctime)s - %(levelname)s - %(name)s - %(message)s‘)    streamhandler.setFormatter(formatter)    logger.addHandler(streamhandler)    logger.error(message)if __name__ == ‘__main__‘:    log(‘hi‘)    log(‘hi too‘)    log(‘hi three‘)

Pre-modification Output results:

2016-07-08 09:17:29,740-error-testlog-hi
2016-07-08 09:17:29,740-error-testlog-hi too
2016-07-08 09:17:29,740-error-testlog-hi too
2016-07-08 09:17:29,740-error-testlog-hi Three
2016-07-08 09:17:29,740-error-testlog-hi Three
2016-07-08 09:17:29,740-error-testlog-hi Three

Modified Sample code:

import loggingdef log(message):    logger = logging.getLogger(‘testlog‘)    streamhandler = logging.StreamHandler()    streamhandler.setLevel(logging.ERROR)    formatter = logging.Formatter(‘%(asctime)s - %(levelname)s - %(name)s - %(message)s‘)    streamhandler.setFormatter(formatter)    logger.addHandler(streamhandler)    logger.error(message)    #  添加下面一句,在记录日志之后移除句柄    logger.removeHandler(streamhandler)if __name__ == ‘__main__‘:    log(‘hi‘)    log(‘hi too‘)    log(‘hi three‘)

Modified output Result:

2016-07-08 09:32:28,206-error-testlog-hi
2016-07-08 09:32:28,206-error-testlog-hi too
2016-07-08 09:32:28,206-error-testlog-hi Three

Depth Analysis:

After Google, you probably figure out that the second time you call log, you get the same logger based on the name of GetLogger, and this logger has the first handler you've added, The second call adds a handler, so the logger has two of the same handler, and so on, a few calls will have several handler.

So here are a few workarounds:

    1. Each time you create a logger with a different name, each time it is a new logger, there is no problem adding multiple handler. PS: That's a stupid way to do it, but that's what I did before. )
    2. Call RemoveHandler () to remove the handler from the logger after each log has been recorded as above.
    3. In the log method, if the logger already has handler, the handler is no longer added.
    4. Same as Method 2, but remove the handler from the Logger handler list with pop.

The following is a code example of Method 3 and Method 4:

Method 3:

import loggingdef log(message):    logger = logging.getLogger(‘testlog‘)    #  这里进行判断,如果logger.handlers列表为空,则添加,否则,直接去写日志    if not logger.handlers:        streamhandler = logging.StreamHandler()        streamhandler.setLevel(logging.ERROR)        formatter = logging.Formatter(‘%(asctime)s - %(levelname)s - %(name)s - %(message)s‘)        streamhandler.setFormatter(formatter)        logger.addHandler(streamhandler)    logger.error(message)if __name__ == ‘__main__‘:    log(‘hi‘)    log(‘hi too‘)    log(‘hi three‘)

Method 4:

import loggingdef log(message):    logger = logging.getLogger(‘testlog‘)    streamhandler = logging.StreamHandler()    streamhandler.setLevel(logging.ERROR)    formatter = logging.Formatter(‘%(asctime)s - %(levelname)s - %(name)s - %(message)s‘)    streamhandler.setFormatter(formatter)    logger.addHandler(streamhandler)    logger.error(message)    #  用pop方法把logger.handlers列表中的handler移除,注意如果你add了多个handler,这里需多次pop,或者可以直接为handlers列表赋空值    logger.handlers.pop()    # logger.handler = []if __name__ == ‘__main__‘:    log(‘hi‘)    log(‘hi too‘)    log(‘hi three‘)
转载:51858817

Python Logging Duplicate Write log issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.