[Tutor] making a log file

Ertl, John John.Ertl at fnmoc.navy.mil
Tue May 18 15:16:42 EDT 2004


All,

I am trying to make a log file that contains different formats for different
types of events.  Because this log file will be for a web service I cannot
use my old method of just directing standard out to a file.  I am trying to
use the logging module but will little success.  I have tried to have two
separate classes that log to the same file but the output prints out the
info for both formats each time the logger is called.  For example I have
two formats one for a start and the other for an event.  The start format
should only be printed when the startLog in invoked but both the startLog
and eventLog print.  The same thing happens when I called the event
log...both the startLog and Event log formats get printed to the log file.  

I have tired many iterations of this idea (many made no sense but I tried
anyways) including just having the separate formats in separate classes and
having almost everything else in the fnmocLog class. But cannot find a way
to have one log file with different formats for different types of events. 
Below is the simple code that I am testing with.  The two formats are very
similar for the test but will be larger and very different in the real world
if I can get this to work.

Any help is appreciated.

************************************************************
import logging
import sys
import time

class fnmocLog:
    def __init__ (self):
        self.name = "test"
        self.platform = "ATOS2"
        self.devlevel = "beta"
        self.productid = int(time.time())
        self.userid = self.productid
##        self.logger = logging.getLogger('tmdServer')
##        self.hdlr = logging.FileHandler('test.log')
##        self.formatter = logging.Formatter('%(asctime)s %(levelname)s
%(message)s ')
##        self.hdlr.setFormatter(self.formatter)
##        self.logger.addHandler(self.hdlr)
##        self.logger.setLevel(logging.INFO)
        
class startLog(fnmocLog):
    def __init__ (self):
        fnmocLog.__init__(self)
        self.logger = logging.getLogger('tmdServer')
        self.hdlr = logging.FileHandler('test.log')
        self.formatter = logging.Formatter('<APPSTART %(asctime)s
%(levelname)s %(message)s >')        
        self.hdlr.setFormatter(self.formatter)
        self.logger.addHandler(self.hdlr)
        self.logger.setLevel(logging.INFO)

class eventLog(fnmocLog):
    def __init__ (self):
        fnmocLog.__init__(self)
        self.logger = logging.getLogger('tmdServer')
        self.hdlr = logging.FileHandler('test.log')
        self.formatter = logging.Formatter('<APPEVENT %(asctime)s
%(levelname)s %(message)s >')        
        self.hdlr.setFormatter(self.formatter)
        self.logger.addHandler(self.hdlr)
        self.logger.setLevel(logging.INFO)        

idstart = startLog()
idevent = eventLog()

idstart.logger.info('This is a test')
#time.sleep(5)
idevent.logger.info('This is the second test')
#time.sleep(20)
idevent.logger.info('This is the third test')

*************** log file 
<APPSTART 2004-05-18 19:01:12,529 INFO This is a test >
<APPEVENT 2004-05-18 19:01:12,529 INFO This is a test >
<APPSTART 2004-05-18 19:01:12,529 INFO This is the second test >
<APPEVENT 2004-05-18 19:01:12,529 INFO This is the second test >
<APPSTART 2004-05-18 19:01:12,529 INFO This is the third test >
<APPEVENT 2004-05-18 19:01:12,529 INFO This is the third test >

As you see I invoke one start log and  two events but I get the APPSTART and
APPEVENT each time either is called.

John C. Ertl
Fleet Numerical Meteorology & Oceanography Center
7 Grace Hopper Ave
Monterey, CA 93943
phone: (831) 656-5704
fax:   (831) 656-4363




More information about the Tutor mailing list