使用以下配置,我的日志文件将被命名为“test-debug.log”,每次运行脚本时它都会无限增长。我只想让这个日志文件包含最近一次运行脚本的日志记录。在重新开始之前,应该删除日志。
我该怎么做呢?
我该怎么做呢?
logger = logging.getLogger('test') #Create a log with the same name as the script that created it
logger.setLevel('DEBUG')
#Create handlers and set their logging level
filehandler_dbg = logging.FileHandler(logger.name + '-debug.log')
filehandler_dbg.setLevel('DEBUG')
#Create custom formats of the logrecord fit for both the logfile and the console
streamformatter = logging.Formatter(fmt='%(levelname)s:\t%(threadName)s:\t%(funcName)s:\t\t%(message)s', datefmt='%H:%M:%S') #We only want to see certain parts of the message
#Apply formatters to handlers
filehandler_dbg.setFormatter(streamformatter)
#Add handlers to logger
logger.addHandler(filehandler_dbg)
fileIO
语法之类的东西? - Paul Hlogging.basicConfig
,添加filemode='w'
会产生相同的效果。 - Ken Shirriff