[Tutor] making a log file

Ertl, John John.Ertl at fnmoc.navy.mil
Wed May 19 10:16:07 EDT 2004

You hit the nail on the head.  I fell into the simple minded trap of
"myapp".  Thank you for the code idea, I like how you structured it.

John Ertl 

-----Original Message-----
From: Rich Krauter [mailto:rmkrauter at yahoo.com]
Sent: Tuesday, May 18, 2004 16:55
To: tutor at python.org
Subject: Re: [Tutor] making a log file

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
> 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
> 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
> 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
> 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)
        # 'name' in fmt refers to name passed to
        # getLogger, not self.name.
        self.formatter = logging.Formatter(fmt)

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

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

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.


Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list