gpt4 book ai didi

python-2.7 - 有没有办法覆盖 python 2.x 中的日志文件

转载 作者:行者123 更新时间:2023-12-03 06:57:34 25 4
gpt4 key购买 nike

我正在使用 python2.x 日志记录模块,例如,

logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
filename='logs.log',
level=logging.INFO)

我希望我的程序在每次执行脚本时覆盖logs.log 文件,目前它只是附加到旧日志中。我知道下面的代码会被覆盖,但如果有办法通过日志配置来做到这一点,它看起来会更好。

with open("logs.log", 'w') as file:
pass

最佳答案

filemode选项添加到basicConfig:

logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
filename='logs.log',
filemode='w',
level=logging.INFO)

来自logging basicConfig 方法的文档(在解释所有选项的大表中):

filemode: Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to ‘a’).

关于python-2.7 - 有没有办法覆盖 python 2.x 中的日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com