54

Using the configuration below, my logfile will be called 'test-debug.log' and it will grow infinitely for everytime I run the script. I just want this logfile to contain the log records from the most recent run of the script. The log should be deleted before starting again.

How do I do that?

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)
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Paul H
  • 901
  • 2
  • 7
  • 12

2 Answers2

68

Try this:

filehandler_dbg = logging.FileHandler(
   logger.name + '-debug.log', 
   mode='w')

to open the filename in write mode instead of append mode, clobbering logger.name

More information: logging.FileHandler docs, open() and list of modes

Ahmad
  • 8,811
  • 11
  • 76
  • 141
dmcc
  • 2,519
  • 28
  • 30
  • Thanks, It now works perfectly.. I had actually looked at the documentation for the `filehandler`, but there was no further information about what and why `mode=a` was the default.. Now I think I understand that it comes from `fileIO` syntax or something like that? – Paul H Feb 06 '14 at 01:56
  • 2
    Exactly. There's a list of all the modes here: http://docs.python.org/2/library/functions.html#open – dmcc Feb 06 '14 at 02:05
  • 1
    its a shame that the docs for logging don't link back to that functions#open page, that would be really useful. – nick fox Feb 16 '18 at 10:30
  • 5
    If you're using `logging.basicConfig`, adding `filemode='w'` has the same effect. – Ken Shirriff Jun 05 '20 at 23:30
2

If you are using any external configuration file then pass 'w' for write mode in args of the handler.

[loggers]
keys=root

[logger_root]
level=DEBUG
handlers=file_handler

[handlers]
keys=file_handler

[handler_file_handler]
class=FileHandler
formatter=formatter
args=("../log/test" + time.strftime("%%Y%%m%%d%%H%%M%%S") + '.log', 'w')

[formatters]
keys=formatter

[formatter_formatter]
format=%(asctime)s %(levelname)-5s [%(module)s] %(message)s
Sumit Kumar
  • 125
  • 1
  • 5