Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Appending a log file and see progress as program executes


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...

On May 30, 1:03 pm, "Karim Ali" <kak@hotmail.com> wrote:

> 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

On 2007-05-30, Karim Ali <kak@hotmail.com> wrote:

> 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            

Karim Ali schrieb:

install cygwin bash, and use

tail out.log

Diez

On May 30, 1:29 pm, Matteo <mah@ncsa.uiuc.edu> wrote:
> - 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?"
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 ----------------

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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc