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 RotatingFileHandler
Replace 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.
Instead of using the logger module, would you be able to use the below instead?
open("result_log.txt", "w").write("result")
I understand this would be a workaround for now, until someone is able to answer this question better.
Hope this helps :)
In fact, you can set filemode
to w
from within your configuration file. You just need to remove the explicit argument naming, like so:
args=('result_log.txt','w')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.