Separate output for log file and stdout

Discussion in 'Python' started by amit.uttam@gmail.com, May 17, 2008.

  1. Guest

    Hey everyone,

    I've recently jumped big time into python and I'm working on a
    software program for testing automation. I had a question about proper
    logging of output. What I would like is:

    1. a function syslog (to log output to log file only)
    2. a function stdout (to log output to stdout only)
    3. a function sslog (to log output to both log and stdout)

    Right now I am using StandOut module http://www.voidspace.org.uk/python/standout.html

    It is quite useful but I can't seem to get good fine grained control
    as my requirements.

    How do the rest of you guys (gals) do this logging?

    Thanks,
    Amit
    , May 17, 2008
    #1
    1. Advertising

  2. Guest

    On May 16, 6:37 pm, Ben Finney <>
    wrote:
    > writes:
    > > I've recently jumped big time into python and I'm working on a
    > > software program for testing automation.

    >
    > Welcome, to both fields :)
    >


    Thanks! I am having a great time learning and coding in python. It's
    an amazing programming language.

    > > I had a question about proper logging of output. What I would like
    > > is:

    >
    > > 1. a function syslog (to log output to log file only)
    > > 2. a function stdout (to log output to stdout only)
    > > 3. a function sslog (to log output to both log and stdout)

    >
    > > Right now I am using StandOut module

    >
    > Have you investigated the 'logging' module in the Python standard
    > library <URL:http://www.python.org/doc/lib/module-logging>? It appears
    > to meet your listed requirements, without need of external
    > dependencies.


    Hmm..Yeah I didn't realize python had its own standard logging
    facility. I took a look at it and it seems to do the job. However, the
    output seems to be syslog style output. In the program I am writing,
    the log file stores all the output of the commands being run (e.g. tcl
    scripts, etc). Will this be possible using the built in python logging
    module?

    Thanks,
    Amit
    , May 19, 2008
    #2
    1. Advertising

  3. Kam-Hung Soh Guest

    On Tue, 20 May 2008 06:58:28 +1000, <> wrote:

    > On May 16, 6:37 pm, Ben Finney <>
    > wrote:
    >> writes:
    >> > I've recently jumped big time into python and I'm working on a
    >> > software program for testing automation.

    >>
    >> Welcome, to both fields :)
    >>

    >
    > Thanks! I am having a great time learning and coding in python. It's
    > an amazing programming language.
    >
    >> > I had a question about proper logging of output. What I would like
    >> > is:

    >>
    >> > 1. a function syslog (to log output to log file only)
    >> > 2. a function stdout (to log output to stdout only)
    >> > 3. a function sslog (to log output to both log and stdout)

    >>
    >> > Right now I am using StandOut module

    >>
    >> Have you investigated the 'logging' module in the Python standard
    >> library <URL:http://www.python.org/doc/lib/module-logging>? It appears
    >> to meet your listed requirements, without need of external
    >> dependencies.

    >
    > Hmm..Yeah I didn't realize python had its own standard logging
    > facility. I took a look at it and it seems to do the job. However, the
    > output seems to be syslog style output. In the program I am writing,
    > the log file stores all the output of the commands being run (e.g. tcl
    > scripts, etc). Will this be possible using the built in python logging
    > module?
    >
    > Thanks,
    > Amit
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    You can define the format of the log output in basicConfig(), for example:

    from logging import basicConfig, error, info, INFO
    ....
    basicConfig(
    datefmt='%Y%m%d_T%H%M%S',
    filemode='a',
    filename=LOG_PATH,
    format='%(asctime)s,%(levelname)s,%(message)s',
    level=INFO
    )

    If you want to log the output of other commands in your log file, you can
    connect a pipe to that command. Maybe look at "subprocess -- Subprocess
    management" in http://docs.python.org/lib/module-subprocess.html

    --
    Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
    Kam-Hung Soh, May 20, 2008
    #3
  4. Guest

    On May 19, 4:05 pm, "Kam-Hung Soh" <> wrote:
    > On Tue, 20 May 2008 06:58:28 +1000, <> wrote:
    > > On May 16, 6:37 pm, Ben Finney <>
    > > wrote:
    > >> writes:
    > >> > I've recently jumped big time into python and I'm working on a
    > >> > software program for testing automation.

    >
    > >> Welcome, to both fields :)

    >
    > > Thanks! I am having a great time learning and coding in python. It's
    > > an amazing programming language.

    >
    > >> > I had a question about proper logging of output. What I would like
    > >> > is:

    >
    > >> > 1. a function syslog (to log output to log file only)
    > >> > 2. a function stdout (to log output to stdout only)
    > >> > 3. a function sslog (to log output to both log and stdout)

    >
    > >> > Right now I am using StandOut module

    >
    > >> Have you investigated the 'logging' module in the Python standard
    > >> library <URL:http://www.python.org/doc/lib/module-logging>? It appears
    > >> to meet your listed requirements, without need of external
    > >> dependencies.

    >
    > > Hmm..Yeah I didn't realize python had its own standard logging
    > > facility. I took a look at it and it seems to do the job. However, the
    > > output seems to be syslog style output. In the program I am writing,
    > > the log file stores all the output of the commands being run (e.g. tcl
    > > scripts, etc). Will this be possible using the built in python logging
    > > module?

    >
    > > Thanks,
    > > Amit
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list

    >
    > You can define the format of the log output in basicConfig(), for example:
    >
    > from logging import basicConfig, error, info, INFO
    > ...
    > basicConfig(
    > datefmt='%Y%m%d_T%H%M%S',
    > filemode='a',
    > filename=LOG_PATH,
    > format='%(asctime)s,%(levelname)s,%(message)s',
    > level=INFO
    > )
    >
    > If you want to log the output of other commands in your log file, you can
    > connect a pipe to that command. Maybe look at "subprocess -- Subprocess
    > management" inhttp://docs.python.org/lib/module-subprocess.html
    >
    > --
    > Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>


    Thanks for the reply.

    Yeah I am using the subprocess module and I am able to capture the
    output and save it to log a file. However, looking at module-logging
    the output format is like syslog. What I want is my own custom log
    file format. I wrote a separate module containing all the functions
    for the logging but I can't seem to use it globally. How does one use
    global variables in python?

    Code:

    # log.py - A base class that provides logging functionality.
    #
    # Copyright 2008 Amit Uttamchandani <>
    #

    import os
    import sys

    logfile = None
    global logfile

    def createLogFile(filename):
    '''Opens a file for logging.'''
    try:
    logfile = open(filename, 'w')
    except IOError:
    print 'Error: Cannot create log file: %s' %
    os.abspath(logfile)
    sys.exit(0)

    def stdlog(logstr):
    '''Writes output to stdout.'''
    sys.stdout.write(logstr)

    def syslog(logstr):
    '''Writes output to logfile.'''
    logfile.write(logstr)

    def agglog(logstr):
    '''Aggregate output of logstr.'''
    syslog(logstr)
    stdlog(logstr)

    def closeLogFile():
    '''Closes the log file.'''
    logfile.close()


    Thanks,
    Amit
    , May 21, 2008
    #4
  5. Guest

    On May 19, 4:05 pm, "Kam-Hung Soh" <> wrote:
    > On Tue, 20 May 2008 06:58:28 +1000, <> wrote:
    > > On May 16, 6:37 pm, Ben Finney <>
    > > wrote:
    > >> writes:
    > >> > I've recently jumped big time into python and I'm working on a
    > >> > software program for testing automation.

    >
    > >> Welcome, to both fields :)

    >
    > > Thanks! I am having a great time learning and coding in python. It's
    > > an amazing programming language.

    >
    > >> > I had a question about proper logging of output. What I would like
    > >> > is:

    >
    > >> > 1. a function syslog (to log output to log file only)
    > >> > 2. a function stdout (to log output to stdout only)
    > >> > 3. a function sslog (to log output to both log and stdout)

    >
    > >> > Right now I am using StandOut module

    >
    > >> Have you investigated the 'logging' module in the Python standard
    > >> library <URL:http://www.python.org/doc/lib/module-logging>? It appears
    > >> to meet your listed requirements, without need of external
    > >> dependencies.

    >
    > > Hmm..Yeah I didn't realize python had its own standard logging
    > > facility. I took a look at it and it seems to do the job. However, the
    > > output seems to be syslog style output. In the program I am writing,
    > > the log file stores all the output of the commands being run (e.g. tcl
    > > scripts, etc). Will this be possible using the built in python logging
    > > module?

    >
    > > Thanks,
    > > Amit
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list

    >
    > You can define the format of the log output in basicConfig(), for example:
    >
    > from logging import basicConfig, error, info, INFO
    > ...
    > basicConfig(
    > datefmt='%Y%m%d_T%H%M%S',
    > filemode='a',
    > filename=LOG_PATH,
    > format='%(asctime)s,%(levelname)s,%(message)s',
    > level=INFO
    > )
    >
    > If you want to log the output of other commands in your log file, you can
    > connect a pipe to that command. Maybe look at "subprocess -- Subprocess
    > management" inhttp://docs.python.org/lib/module-subprocess.html
    >
    > --
    > Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>


    Thanks for the reply.

    Yeah I am using the subprocess module and I am able to capture the
    output and save it to log a file. However, looking at module-logging
    the output format is like syslog. What I want is my own custom log
    file format. I wrote a separate module containing all the functions
    for the logging but I can't seem to use it globally. How does one use
    global variables in python?

    Code:

    # log.py - A base class that provides logging functionality.
    #
    # Copyright 2008 Amit Uttamchandani <>
    #

    import os
    import sys

    logfile = None
    global logfile

    def createLogFile(filename):
    '''Opens a file for logging.'''
    try:
    logfile = open(filename, 'w')
    except IOError:
    print 'Error: Cannot create log file: %s' %
    os.abspath(logfile)
    sys.exit(0)

    def stdlog(logstr):
    '''Writes output to stdout.'''
    sys.stdout.write(logstr)

    def syslog(logstr):
    '''Writes output to logfile.'''
    logfile.write(logstr)

    def agglog(logstr):
    '''Aggregate output of logstr.'''
    syslog(logstr)
    stdlog(logstr)

    def closeLogFile():
    '''Closes the log file.'''
    logfile.close()


    Thanks,
    Amit
    , May 21, 2008
    #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. EvgueniB
    Replies:
    1
    Views:
    614
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    430
    Jean-Francois Briere
    Jun 27, 2006
  3. Mike
    Replies:
    7
    Views:
    568
  4. Une Bévue
    Replies:
    1
    Views:
    135
    Justin Collins
    Feb 27, 2008
  5. Replies:
    6
    Views:
    164
    Arlen Cuss
    Mar 14, 2008
Loading...

Share This Page