python - Custom Log File That Doesn't Grow Past a Given Size -


i need write output log file every time events in application triggered. right i'm doing in simplest way imagineable:

with open('file.log', 'a+') file:     file.write('some info') 

this works, problem don't want file grow indefinitely. want hard size cutoff of, 25 mb. don't want clear file when reaches size.

instead, want function how believe other log files work, clearing oldest data top of file , appending new data end of file. approach achieving in python? note can't use python's logging library because celery application , (a) have logging set purposes unrelated , (b) celery , logging library not play @ all.

import os statinfo = os.stat('somefile.txt') 

this return size of somefile.txt in bytes. can like:

if statinfo.st_size > 25000000: 

you can implement want. read number of lines going replaced, delete , save reminder in temporary file, write data wish, , append temporary file. not effective, work.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -