By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

Appending a log file and see progress as program executes

P: n/a
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/result...ca&FORM=SERNEP

May 30 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On May 30, 1:03 pm, "Karim Ali" <kak...@hotmail.comwrote:
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

May 30 '07 #2

P: n/a
On 2007-05-30, Karim Ali <ka****@hotmail.comwrote:
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()

--
Grant Edwards grante Yow! Do I have a lifestyle
at yet?
visi.com
May 30 '07 #3

P: n/a
Karim Ali schrieb:
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
May 30 '07 #4

P: n/a
On May 30, 1:29 pm, Matteo <mah...@ncsa.uiuc.eduwrote:
- 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?"

May 30 '07 #5

P: n/a
Diez B. Roggisch wrote:
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 ----------------

May 30 '07 #6

P: n/a
Steve Holden schrieb:
Diez B. Roggisch wrote:
>Karim Ali schrieb:
[...]
>>
install cygwin bash, and use

tail out.log
I think Diez meant

tail -f out.log
I did. Thanks.

diez
May 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.