473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Separate output for log file and stdout

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
Jun 27 '08 #1
5 3299
am********@gmail.com writes:
I've recently jumped big time into python and I'm working on a
software program for testing automation.
Welcome, to both fields :-)
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.

--
\ "I went to the hardware store and bought some used paint. It |
`\ was in the shape of a house." -- Steven Wright |
_o__) |
Ben Finney
Jun 27 '08 #2
On May 16, 6:37 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
amit.ut...@gmail.com 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
Jun 27 '08 #3
On Tue, 20 May 2008 06:58:28 +1000, <am********@gmail.comwrote:
On May 16, 6:37 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>amit.ut...@gmail.com 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>

Jun 27 '08 #4
On May 19, 4:05 pm, "Kam-Hung Soh" <kamhung....@gmail.comwrote:
On Tue, 20 May 2008 06:58:28 +1000, <amit.ut...@gmail.comwrote:
On May 16, 6:37 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
amit.ut...@gmail.com 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 <au************@canoga.com>
#

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
Jun 27 '08 #5
On May 19, 4:05 pm, "Kam-Hung Soh" <kamhung....@gmail.comwrote:
On Tue, 20 May 2008 06:58:28 +1000, <amit.ut...@gmail.comwrote:
On May 16, 6:37 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
amit.ut...@gmail.com 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 <au************@canoga.com>
#

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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
2
by: silly | last post by:
/*Thanks again to thos who helped with my 'more hand written integer pow() functions (LONG POST)' query. I needed to write a function to write out integers and after looking at some stuff on...
3
by: Peter Ammon | last post by:
Let's say I want to call a function but suppress what it writes to stdout. One way that works on my machine is this: #include <stdio.h> int main(void) { freopen("/dev/null", "w", stdout);...
20
by: Ilias Lazaridis | last post by:
IDLE has an output format like this: <type 'object'> <type 'type'> <type 'type'> How can I customize it to become like that: <type 'object'> <type 'type'>
3
by: pnsreee | last post by:
Hi All, I am trying to print a log file created in the same script with the code bellow. but Im not getting output on screen. The output file is created using rediretion of output. ...
13
by: Jim Langston | last post by:
I had asked this in comp.lang.c++ with out any answers that would actually work, so I'm hoping someone here may know a way. I am calling C library functions that want to output to stdout. I need...
45
by: Dennis | last post by:
Hi, I have a text file that contents a list of email addresses like this: "foo@yahoo.com" "tom@hotmail.com" "jerry@gmail.com" "tommy@apple.com" I like to
1
by: Anh Khuong | last post by:
Hi all, I am using pexpect and I want to send output of pexpet to both stdout and log file concurrently. Anybody know a solution for it please let me know. Thanks
5
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.