Analysis of the problem that logging will repeat the log in python
Phenomenon
The students of Xiaoqiang python full-stack automation test class asked that there would be repeated log writing, as follows
Reason
When you call log for the second time, get the same logger according to the name in getLogger (name), and this logger already has the handler you added the first time, and another handler is added in the second call, that is to say, there are two identical handler in the logger, so there will be several handler after several calls.
Solution
After logging, removeHandler
For example: logger.removeHandler (streamhandler)
In addition, you can also determine that if the logger.handlers list is empty, add it, otherwise, write the log directly, for example
If not logger.handlers: xxxxlogger.error ('Baidu search: Xiaoqiang Test Brand')