CSC111 logging111.py
--D. Thiebaut 00:20, 11 March 2010 (UTC)
# logging111.py
# D. Thiebaut
# a simple logging utility that offers a replacement for print
# and raw_input with the functions Print() and Raw_input() so
# that the interaction between the user and the comptuer can be
# fully captured.
#
# To use this facility, one need to do 3 things:
# import two functions from the module
# replace all print statements by a call to Print()
# replace all raw_input statements by a call to Raw_input()
#
# Example:
#
# #original program
# print "hello, how are you?"
# answer = raw_input( "> " )
# print "bye"
#
# #modified program
# from logging111 import Print
# from logging111 import Raw_input
# Print( "hello, how are you?" )
# answer = Raw_input( "> " )
# Print( "bye" )
#
import logging
#--- define the log file name ---
logName = "eliza"
logFileName = logName+'.log'
#--- create a logging object called logger ---
logger = logging.getLogger( logName )
hdlr = logging.FileHandler( logFileName )
#--- set the format to be just the message ---
#formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
#--- log all levels above and including info messages ---
logger.setLevel(logging.INFO)
#--- clear the log file when starting ---
logFile = open( logFileName, "w" )
logFile.write( "" )
logFile.close()
#--- the main log function. Appends the string s to the log ---
def log( s ):
logger.info( s )
def Print( s1, s2=None, s3=None, s4=None, s5=None, s6=None ):
s = s1
if s2 is not None: s += " " + str( s2 )
if s3 is not None: s += " " + str( s3 )
if s4 is not None: s += " " + str( s4 )
if s5 is not None: s += " " + str( s5 )
if s6 is not None: s += " " + str( s6 )
print s
log( s )
def Raw_input( s1 ):
ans = raw_input( s1 )
log( s1+ " " +ans )
return ans