[Tutor] making a log file

Rich Krauter rmkrauter at yahoo.com
Tue May 18 19:54:39 EDT 2004


On Tue, 2004-05-18 at 15:16, Ertl, John wrote:
> 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.
> 

Hi John,

I think the trick may be giving different names to different Logger
objects. This is what I tried:


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

    def _setup_logger(self,
        fmt='<%(name)s %(asctime)s %(levelname)s %(message)s>'):
        self.hdlr = logging.FileHandler('%s.log'%self.name)
        self.logger.setLevel(logging.INFO)
        # 'name' in fmt refers to name passed to
        # getLogger, not self.name.
        self.formatter = logging.Formatter(fmt)
        self.hdlr.setFormatter(self.formatter)
        self.logger.addHandler(self.hdlr)    

        
class startLog(fnmocLog):
    def __init__ (self):
        fnmocLog.__init__(self)
        self.logger = logging.getLogger('APPSTART')
        self._setup_logger()

class eventLog(fnmocLog):
    def __init__ (self):
        fnmocLog.__init__(self)
        self.logger = logging.getLogger('APPEVENT')
        self._setup_logger()


idstart = startLog()
idevent = eventLog()

idstart.logger.info('This is a test')
idevent.logger.info('This is the second test')
idevent.logger.info('This is the third test')


I think the example in the docs may be a little misleading - it uses
'myapp' as the name passed to getLogger; I'm guessing that's just
because it's a simple example, but I could be wrong. You have more
complicated requirements, so you probably want to create different
Logger instances by passing different names to getLogger. That way you
can control each Logger instance seperately, rather than trying to make
one Logger instance do all the work. 

Good luck.

Rich





More information about the Tutor mailing list