如何关闭Python中的日志文件

0 投票
1 回答
2224 浏览
提问于 2025-04-17 22:41

我想关闭我的日志文件,因为当我在软件中重新启动一个类时,它会继续写入之前的日志文件。这是代码的一部分:

class nmeaManager(object) :

    def __init__(self,source_file,gps_solution,log_file,update_rate):


        ### Get the Source File 
        ############################################## 
        self.source = source_file

        ### Get the Gps solution depending on the name of the file
        ##############################################
        self.gps_solution = gps_solution

        ### Get the Destination File
        ##############################################
        self.destination = log_file

        ###Set the Destination file
        ##############################################
        logging.basicConfig(filename=self.destination,filemode='w',format= "[%(asctime)s]%(message)s",level=logging.DEBUG)

        #Get update_rate
        self.update_rate = update_rate

        ###Start the ckecking cycle
        ##############################################
        self.start(self.gps_solution,self.update_rate,self.source)
        self.stop(self.destination)

我不知道有没有类似 logging.close() 这样的东西?

1 个回答

0

然后用 w 创建一个新文件,文件名可以加上一些额外的标识,比如:

"project-" + datetime.today().strftime('%Y-%m-%d') + ".log"

撰写回答