Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Analysis of the problem that logging will repeat the log in python

Shulou Source: shulou.com Published: 2022-06-03 03:40:29 10月02日 Update

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')

Tags: Log Xiaoqiang test two that is reason brand student situation scheme time phenomenon first time solution in addition search automation problem analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Shulou Tech Info vpn Xiaomi