在python 3.2中启动和停止记录器 [英] Start and stop logger in python 3.2

查看:110
本文介绍了在python 3.2中启动和停止记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用两个按钮:一个用于开始记录RS422链接消息,另一个用于停止记录.

In my application, I use two buttons: One for START recording of RS422 link messages and one for STOP the record.

第一次,该应用程序正确运行:创建带有消息的.txt文件. 第二次,当我单击开始"按钮时,一个新的. txt文件已创建,但是现在,消息既记录在该文件中,也记录在第一个.txt文件中.

The first time, the application runs correctly : Creation of a .txt file with the messages. The second time, when I click on the START button, a new . txt file is created but, now, messages are recording in this file but also in the first .txt file.

查看我的代码:

def start_clic(self):
    logger=logging.getlogger("CFD")
    stringfilename=datetime.now().strftime('log_%Y_%m_%H_%M.txt')
    filehandler=logging.FileHandler(stringfilename)
    formatter=logging.Formatter('%(asctime)s %(message)s')
    logger.addHandler(filehandler)
    logger.setLevel(logging.INFO)

def stop_clic(self):
    logger.setLevel(logging.WARNING)
    filehandler.close()

有人有主意吗?

推荐答案

如果要停止这样的日志记录,则必须使用

You'll have to remove the handlers if you want to stop logging like that, using the Logger.removeHandler() method:

def stop_clic(self):
    logger=logging.getlogger("CFD")
    logger.setLevel(logging.WARNING)
    logger.removeHandler(filehandler)
    filehandler.close()

否则它将继续使用.

这篇关于在python 3.2中启动和停止记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆