I am writing some logs in a file using logging module
#filename : demo.py
import logging
#other imports as well
logging.basicConfig(filename="myfile.log",
format='%(asctime)s %(funcName)s %(levelname)s %(message)s',
filemode='w')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("Writing logs in this way")
But sometimes my module writes lots of nullvalues
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
So I have written a script to remove these values. But when I modify myfile.log my further logs are not stored. I know that this is due to change/close of file stream of myfile.log. Is it possible to modify my log file without hurting or losing my already existing stream which demo.py has.
I am using ubuntu : 18.04 and python : 3.6.9
For modification I tried the following ways
- I am using script
sed -i -e 's/\o00//g'myfile.log to modify file - I manually edit file using vim :
vim myfile.logTo delete line I usedddthen closed using:wq
From a comment https://stackoverflow.com/questions/64875452/modify-a-file-without-changing-its-file-stream I know that both method over write new file over old file. Is there a way to edit file inplace.
filemode='w'instead ofa. It's like>vs>>in this answer of mine. If something truncates the file then your logger will make the file "regain its old size and grow further, missing data will be filled with zeros (in a sparse way, if possible)". What process truncates the file? I don't know,logrotatemaybe. Also see how this is flawed in general. – Kamil Maciorowski Nov 17 '20 at 15:29filemode='w'. – Kamil Maciorowski Nov 17 '20 at 15:36filemode='a'then it should solve the issue.. right? – Pranjal Doshi Nov 17 '20 at 15:40