logging - How Can I Detect Changes To A Text File During The Execution Of A Python Script? -


i working temperamental web app i'm not going name. runs problems time time, , when does, writes stack traces , error messages exception.log file. want know these problems in timely manner, i've got python script scans log regularly (hooray cron). if size of exception.log greater zero, script dumps contents of file email me, moves them exception_archive.log. current tactic read in file, send email , write exception archive if necessary, , if both of steps successful, just

target = open(target_log, 'w') target.close() 

to zorch original log. however, since can't predict when system write exception.log, there @ least 1 point in script lose data - system write log after i've read existing data , decided overwrite file. also, have learned painful experience if exception.log not exist, temperamental web app not recreate - it'll drop exception data on floor. naïve solution of "rename , re-create log file" pushes problem down layer.

the core of question, then, is: how can (transfer|move|read-write-erase) data 1 text file in such way if new data written file while script executing, there 0 or minimal chance of losing data? suspect either hard problem, or solved problem haven't heard solution to. can't extend app itself, either - management skeptical of tinkering it, plus it's not in python, i'd have start scratch.

additional context:

[me@server ~]$ uname -a linux server.example.com 2.6.9-101.elsmp #1 smp thu jul 21 17:28:56 edt 2011 i686 i686 i386 gnu/linux [me@server ~]$ python  python 2.3.4 (#1, may  5 2011, 17:13:16)  [gcc 3.4.6 20060404 (red hat 3.4.6-11)] on linux2 

it's running on cruddy shared hosting, part of why call "temperamental." call worse things running python 2.3 in 2011. easier if had modern python work with.


i'm going go variation on kevin's answer below - since control crontab, i'm going have script in right timestamp range , operate on that. has side benefit relevant information can live in python script , single source of truth.

i avoid deleting exception log while web app still running. scan log updates without making changes.

#lastknownsizeoffile saved somewhere persists between executions of script if size(file) > lastknownsizeoffile: #found update!     amounttoread = size(file) - lastknownsizeoffile     file.seek(lastknownsizeoffile)     newdata = file.read(amounttoread)     exceptionarchive.write(newdata)     emailme(newdata)     lastknownsizeoffile += amounttoread 

if you're worried log file grow large way, delete periodically during low-activity hours (say, 2 am), when unlikely app writing it.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -