Logging to python file doesn't overwrite file when using the mode='w' argument to FileHandler
Asked Answered
G

5

18

I have some code to set up a log in Python 2.7 (using the logging module):

import os
import logging
logger=logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
log_filename=os.path.join(os.path.dirname(copasi_file),os.path.split(copasi_file)[1][:-4]+'_log.log')
handler=logging.FileHandler(log_filename,mode='w')
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug('debugging message')

This code works and I am getting the output, however I intend to use this log to do a lot of debugging so I want to overwrite the log file each time its run. In the docs say to use the mode keyword argument to the 'FileHandler. It doesn't specify precisely *which* mode to use for overwrite file each time but I think a reasonable assumption would bemode='w'`. This however doesn't work. Can anybody tell me why?

Glamorous answered 16/7, 2016 at 8:57 Comment(3)
I can't reproduce the behavior. I used the code you provided and just changed the file name, the log is overwritten every time I run the code. If I change mode to a then new run adds line to end,Jeffery
This is strange and may be related to a previous question I had. Do you know if it possible to permanently override the logging modules basic behaviour?Glamorous
I don't know a way to do it but it doesn't mean it's impossible,Jeffery
G
9

The problem is that the file doesn't actually get overwritten until a new python shell is started.

Glamorous answered 16/7, 2016 at 9:23 Comment(0)
E
23

This solves problem for me:

handler = logging.FileHandler(log_filename, 'w+')
Everyman answered 27/8, 2018 at 12:18 Comment(0)
G
9

The problem is that the file doesn't actually get overwritten until a new python shell is started.

Glamorous answered 16/7, 2016 at 9:23 Comment(0)
S
5

I am not familiar with this to much, and I did not really see anything that stuck out in google. Have you tried just using:

    handler=logging.FileHandler(log_filename, 'w')
Solingen answered 16/7, 2016 at 9:6 Comment(1)
Thanks, but this behaves the same way.Glamorous
D
0

Replace this line

logger.addHandler(handler)

By this one:

logger.handlers=[handler]
Decoct answered 22/1 at 17:47 Comment(0)
D
0

This worked for me: (missing force=True) The actual answer is to add the filemode = 'w' AND force = True parameter to basicConfig. from https://mcmap.net/q/615817/-is-there-a-way-to-overwrite-log-files-in-python-2-x

Damali answered 4/3 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.