pythonlogging.basicConfig 不会覆盖现有的日志文件

Moj*_*van 3 python logging overwrite python-3.x

我正在使用 Pythonlogging.basicConfig方法,向其传递一些参数,包括filemode = "w". 如果不指定,则默认为"a", 追加。但是,尽管指定它覆盖现有日志文件,但它仍在附加。

import logging
# add filemode "w" to overwrite
logging.basicConfig(filename = "sample.log", level = logging.INFO,filemode = "w")
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
Run Code Online (Sandbox Code Playgroud)

小智 5

使用下面的代码并尝试 -

filemode = "w+"

例子

logging.basicConfig(filename = "sample.log",
                    level = logging.INFO,
                    filemode = "w+" )
Run Code Online (Sandbox Code Playgroud)