python - How do I get logger to delete existing log file before writing to it again? -
using configuration below, logfile called 'test-debug.log' , grow infinitely everytime run script. want logfile contain log records recent run of script. log should deleted before starting again.
how do that?
logger = logging.getlogger('test') #create log same name script created logger.setlevel('debug') #create handlers , set logging level filehandler_dbg = logging.filehandler(logger.name + '-debug.log') filehandler_dbg.setlevel('debug') #create custom formats of logrecord fit both logfile , console streamformatter = logging.formatter(fmt='%(levelname)s:\t%(threadname)s:\t%(funcname)s:\t\t%(message)s', datefmt='%h:%m:%s') #we want see parts of message #apply formatters handlers filehandler_dbg.setformatter(streamformatter) #add handlers logger logger.addhandler(filehandler_dbg)
try this:
filehandler_dbg = logging.filehandler(logger.name + '-debug.log', mode='w')
to open filename in write
mode instead of append
mode, clobbering logger.name
more information: logging.filehandler docs
Comments
Post a Comment