I am trying to config the loggers in a text configuration file using Python. Here is partial content:
[logger_root]
handlers=result
level=NOTSET
[handler_result]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=simple
level=DEBUG
args=('result_log.txt')
I would like to rewrite the log file every time I run the system. But not know how to set it in the file. I try this but fails:
args=('result_log.txt',filemode='w')
A lot of articles talk about how to set it from the Python code. But I would like to set it in the file. How to do it? Thanks.
=======================================
Edit: I can write some info into the log file ‘result_log.txt’ by:
result_logger = logging.getLogger('result')
result_logger.debug("info to write")
But I can only “append” to the file. I would like to rewrite the file every time i run.
You can use either of the two methods:-
Backup the old log file and create a new file using
RotatingFileHandlerReplace the file itself (provided you don’t need the old log anymore). You can have a look at this for the steps
Hope this helps.