From: Philip Semanchuk on

On May 23, 2010, at 9:46 AM, Frank GOENNINGER wrote:

>
> Hi all:
>
> Being completely new to Python still (just about a week into it now) I
> tried to follow the Python 2.6.5 version documemtation aiming at
> setting
> up a logger as follows:
>
> <code>
>
> import logging
>
> global gPIBLogger
>
> class PIBLogger(object):
> '''
> TODO: classdocs
> '''
>
> def __init__(self, logFileName):
> '''
> Constructor
> '''
> self.logFileName = logFileName
> self.logger = logging.getLogger('PIBLogger')
> self.logger.setLevel(logging.DEBUG)
> handler =
> logging.handlers.RotatingFileHandler(self.logFileName,
>
> maxBytes=1000000,
> backupCount=9)
> self.logger.addHandler(handler)
> gPIBLogger = self.logger
>
>
> def main():
> mylogger = PIBLogger('/tmp/pib.log')
> gPIBLogger.debug(' Hi ')
>
> if __name__ == "__main__":
> main()
>
> </code>
>
> When trying to execute main() I get:
>
> Traceback (most recent call last):
> File "/.../src/pib/logging.py", line 37, in <module>
> main()
> File "/.../src/pib/logging.py", line 33, in main
> mylogger = PIBLogger('/tmp/pib.log')
> File "/...src/pib/logging.py", line 23, in __init__
> self.logger = logging.getLogger('PIBLogger')
> AttributeError: 'module' object has no attribute 'getLogger'
>
> I double checked and yes, getLogger is there. Why is the interpreter
> asking for an "attribute" here ? Any hints on what I am doing wrong ?


Short answer: Change the name of src/pib/logging.py to something else.

Long answer: When Python hits the line "import logging", it first
looks in the current directory and imports logging.py, which in this
case is the file it's already executing. It never finds the standard
library's logging module.

One way you could have figured this out would be to add this as the
first line of main():
print dir(logging)

That would have told you what Python thought the logging module looked
like, and would have perhaps recognized it as your own.

Cheers
Philip