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:
- 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. )
- Call RemoveHandler () to remove the handler from the logger after each log has been recorded as above.
- In the log method, if the logger already has handler, the handler is no longer added.
- 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