logging from severl classes

flupke flupke at nonexistingdomain.com
Wed Dec 22 17:39:00 CET 2004


i'm trying to log to the same file (using logging module) from different 
classes but i can't seem to get it to work.
File 1 is the main file which sets up the logger and then i want to also
use the same logger in another class
This is what i have

import logging

class A:
    def __init__(self):
	self.logger = logging.getLogger("app_logfile")
         ### stdout handler ###

         stream_hld = logging.StreamHandler()
         stream_formatter =
		logging.Formatter('%(asctime)s %(levelname)s %(module)s 			%(lineno)d 
%(message)s ')

import logging

class B:
    def __init__(self):
	self.logger = logging.getLogger("app_logfile")
         self.logger.debug("creating class B")

    def otherfunction(self):
	self.logger.debug("in otherfunction")

But i get an error:
   AttributeError: B instance has no attribute 'logger'

I was hoping that by requesting a logger with the same name 
(app_logfile) i would get the setup also and could use the logger 
straight away. Funny thing is that the use of the logger in the __init__ 
function of class B works but the call to the logger in otherfunction 
doesn't work and results in the error above.
Class B is created after class A so the logger object exists already.

Any ideas of what i'm doing wrong and how i can achieve setting up the 
logger object only once?


More information about the Python-list mailing list