Click on the link to join QQ Group 522720170 (free public classes, videos have everything):https://jq.qq.com/?_wv=1027&k=5C08ATe
Phenomenon
The students of the test class of the full stack of jack-up Python ask that there will be repeated write logs, as follows
Reason
When you call log for the second time, you get the same logger based on the name of GetLogger, and the logger already has the first handler you added, and the second call adds a handler, That means there are two of the same handler in this logger, so there will be several calls to the handler.
Solution Solutions
After logging is complete removehandler
Example: Logger.removehandler (Streamhandler)
In addition, you can also go to the judgment, if the Logger.handlers list is empty, then add, otherwise, directly to write the log, for example
If not logger.handlers:xxxxlogger.error (' Baidu search: Cockroach test brand ')
Analysis of the problem of logging repeating log in Python