Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report