Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logging.basicConfig doesn't overwrite the existing log file

I'm using Python logging.basicConfig method, passing it some arguments including filemode = "w". If it's not specified, it defaults to "a", appending. However, despite specifying it to overwrite the existing log file, it's yet appending.

import logging
# add filemode "w" to overwrite
logging.basicConfig(filename = "sample.log", level = logging.INFO,filemode = "w")
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
like image 766
Mojtaba Abdi Khassevan Avatar asked Jan 24 '26 01:01

Mojtaba Abdi Khassevan


1 Answers

Use the below code and try -

filemode = "w+"

Example

logging.basicConfig(filename = "sample.log",
                    level = logging.INFO,
                    filemode = "w+" )
like image 71
Pulakesh Dev Das Avatar answered Jan 25 '26 14:01

Pulakesh Dev Das



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!