Appending a log file and see progress as program executes

K

Karim Ali

Hi,

I am writing a program that will take several days to execute :) and would
like to append to a log file but be able to open that file at any time and
see the errors that have occured.

So this is what I am doing:

----------------------------------------------
flog = open('out.log', 'a')
.....
when needed:
sys.stdout=flog
print "error message"
------------------------------------------------

This will print directly to log. I use sys.stdout so i can quickly (in code)
change back and forth between errors displayed on screen and errors logged..

This works great. The only problem is that I cant see anything in the log
file when I try to open it say with notepad while the program is
running...and this is not good at all!

Any suggestions are appreciated.

Karim

_________________________________________________________________
See Fireworks On Live Image Search
http://search.live.com/images/results.aspx?q=Fireworks&mkt=en-ca&FORM=SERNEP
 
M

Matteo

Hi,

I am writing a program that will take several days to execute :) and would
like to append to a log file but be able to open that file at any time and
see the errors that have occured.

So this is what I am doing:

----------------------------------------------
flog = open('out.log', 'a')
....
when needed:
sys.stdout=flog
print "error message"
--------------------------------------------

I imagine that your problem is that stdout is buffered, and hence only
writes to the output when the buffer is full. To ameliorate this, you
can use flog.flush() after every print statement.
Other alternatives:
- Use stderr for printing log messages, and run python in unbuffered
mode (python -u script.py)
You can store the log file by redirecting stderr. Using bash, this
would be:
python -u script.py 2>out.log

- Write an autoflush class:
class AutoFlush:
def __init__(self, stream):
self.stream = stream
def write(self, text):
self.stream.write(text)
self.stream.flush()

...
flog=open('out.log','a')
flog=AutoFlush(flog)

print >>flog,"I'm a message!"

Note that instead of changing stdout, you can also print directly to a
file with:
print >>flog,"Something to print"

cheers,
-matt
 
G

Grant Edwards

Hi,

I am writing a program that will take several days to execute :) and would
like to append to a log file but be able to open that file at any time and
see the errors that have occured.

So this is what I am doing:

----------------------------------------------
flog = open('out.log', 'a')
....
when needed:
sys.stdout=flog
print "error message"

sys.stdout.flush()
 
D

Diez B. Roggisch

Karim said:
Hi,

I am writing a program that will take several days to execute :) and
would like to append to a log file but be able to open that file at any
time and see the errors that have occured.

So this is what I am doing:

----------------------------------------------
flog = open('out.log', 'a')
....
when needed:
sys.stdout=flog
print "error message"
------------------------------------------------

This will print directly to log. I use sys.stdout so i can quickly (in
code) change back and forth between errors displayed on screen and
errors logged..

This works great. The only problem is that I cant see anything in the
log file when I try to open it say with notepad while the program is
running...and this is not good at all!

Any suggestions are appreciated.

install cygwin bash, and use

tail out.log


Diez
 
M

Matteo

- Write an autoflush class:
class AutoFlush:
def __init__(self, stream):
self.stream = stream
def write(self, text):
self.stream.write(text)
self.stream.flush()

...
flog=open('out.log','a')
flog=AutoFlush(flog)

print >>flog,"I'm a message!"
Oops! According to another thread, this won't work for files (but will
work for stdio/stderr)
Check the thread entitled "print bypasses calling write method for
objects inheriting from file?"
 
S

Steve Holden

Diez said:
Karim Ali schrieb: [...]

install cygwin bash, and use

tail out.log
I think Diez meant

tail -f out.log

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top