Logging output from python

Discussion in 'Python' started by Barry, Dec 8, 2006.

  1. Barry

    Barry Guest

    Hi, guys

    Basiclly, it is automated testing system. There is a main python script
    that handles the testing campagin. This main script will call another
    script that will in turn runs a few hundered individual python scripts.


    Here is my problem. I want to log everything displayed in the screen
    after I start the main python script. Things include unhandled
    exceptions , message from print statement and other sources.
    Basically, if it is displayed on the screen, I want to log it..


    It might not be a pythons specific problem. Does anyone know a small
    tool does that job?


    Thanks.
    Barry, Dec 8, 2006
    #1
    1. Advertising

  2. Barry wrote:
    > Hi, guys
    >
    > Basiclly, it is automated testing system. There is a main python script
    > that handles the testing campagin. This main script will call another
    > script that will in turn runs a few hundered individual python scripts.
    >
    >
    > Here is my problem. I want to log everything displayed in the screen
    > after I start the main python script. Things include unhandled
    > exceptions , message from print statement and other sources.
    > Basically, if it is displayed on the screen, I want to log it..
    >
    >
    > It might not be a pythons specific problem. Does anyone know a small
    > tool does that job?
    >
    >
    > Thanks.
    >


    If it's on linux you can just redirect the screen output to a file:

    python initialfile.py 1>stdout.txt 2>stderr.txt

    or if you want standard out and standard error to go to the same file:

    python initialfile.py 1>output.txt 2>output.txt

    or if you don't want to see anything on standard error:

    python initialfile.py 1>output.txt 2>/dev/null


    As for windows, I'll test it now...

    It turns out you can at least redirect the output to a file, I'm not
    sure what it does with standard error or even if it exists or not.

    python initialfile.py > output.txt

    should work.


    Hope it helps,

    Cameron.
    Cameron Walsh, Dec 8, 2006
    #2
    1. Advertising

  3. At Thursday 7/12/2006 23:21, Cameron Walsh wrote:

    > > Here is my problem. I want to log everything displayed in the screen
    > > after I start the main python script. Things include unhandled
    > > exceptions , message from print statement and other sources.
    > > Basically, if it is displayed on the screen, I want to log it..

    >
    >If it's on linux you can just redirect the screen output to a file:
    >
    >python initialfile.py 1>stdout.txt 2>stderr.txt
    >[...]
    >
    >As for windows, I'll test it now...
    >
    >It turns out you can at least redirect the output to a file, I'm not
    >sure what it does with standard error or even if it exists or not.
    >
    >python initialfile.py > output.txt


    It's the same syntax as noted for linux above. 1> is the same as > alone.

    If ALL the testing is done on a single program (that is, no os.system
    or spawn or subprocess...) then you could just replace sys.stdout and
    sys.stderr with another open file (or file-like) object.


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
    Gabriel Genellina, Dec 8, 2006
    #3
  4. Barry

    MRAB Guest

    Gabriel Genellina wrote:
    > At Thursday 7/12/2006 23:21, Cameron Walsh wrote:
    >
    > > > Here is my problem. I want to log everything displayed in the screen
    > > > after I start the main python script. Things include unhandled
    > > > exceptions , message from print statement and other sources.
    > > > Basically, if it is displayed on the screen, I want to log it..

    > >
    > >If it's on linux you can just redirect the screen output to a file:
    > >
    > >python initialfile.py 1>stdout.txt 2>stderr.txt
    > >[...]
    > >
    > >As for windows, I'll test it now...
    > >
    > >It turns out you can at least redirect the output to a file, I'm not
    > >sure what it does with standard error or even if it exists or not.
    > >
    > >python initialfile.py > output.txt

    >
    > It's the same syntax as noted for linux above. 1> is the same as > alone.
    >
    > If ALL the testing is done on a single program (that is, no os.system
    > or spawn or subprocess...) then you could just replace sys.stdout and
    > sys.stderr with another open file (or file-like) object.
    >

    Redirection in Windows is explained here:

    http://www.microsoft.com/resources/.../all/proddocs/en-us/redirection.mspx?mfr=true
    MRAB, Dec 9, 2006
    #4
  5. Cameron Walsh schrieb:
    >
    > If it's on linux you can just redirect the screen output to a file:
    >
    > python initialfile.py 1>stdout.txt 2>stderr.txt



    > As for windows, I'll test it now...
    >
    > It turns out you can at least redirect the output to a file, I'm not
    > sure what it does with standard error or even if it exists or not.
    >
    > python initialfile.py > output.txt
    >
    > should work.


    >cmd

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    >type output.py

    import sys

    print 'This is print.'
    sys.stdout.write('This is stdout.write()\n')
    sys.stderr.write('This is stderr.write()\n')

    >python output.py 1>stdout.txt 2>stderr.txt


    >type stdout.txt

    This is print.
    This is stdout.write()

    >type stderr.txt

    This is stderr.write()

    Seems on XP it works too.

    Leonhard
    Leonhard Vogt, Dec 9, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    0
    Views:
    941
    Stefan Siegl
    Aug 27, 2003
  2. janne
    Replies:
    0
    Views:
    9,448
    janne
    Sep 10, 2004
  3. Christoph Haas
    Replies:
    0
    Views:
    432
    Christoph Haas
    Jun 12, 2006
  4. Christoph Haas
    Replies:
    1
    Views:
    441
    Vinay Sajip
    Jun 14, 2006
  5. johnny
    Replies:
    1
    Views:
    625
    Dennis Lee Bieber
    Dec 12, 2006
Loading...

Share This Page